Can I do multi mapping with redbeanphp?

409 views
Skip to first unread message

Jokin

unread,
Mar 17, 2015, 3:22:03 PM3/17/15
to redbe...@googlegroups.com

can I load an object and it's relations with just a query?

so, if I have

select * from parent inner join child on parent.id = child.parent_id

could I get and array of parent beans with is ownChild informed just from that query?

something like multi mapping in dapper: https://github.com/StackExchange/dapper-dot-net#multi-mapping

there are like 30 parent with 3 or 4 child each, which it makes around 120 queries. I know that maybe I could change my model or make the queries on demand. But right now all this info goes to the client and could be edited in there, so I need the full entity. 

I tried also to preload the the childs, making a query witch load al the childs, but it doesn't matter and redbean still make a query for each row. So right now I have to export without the childs and add after, but I would like to know if is there a more pretty way to do so. 

Thanks in advance, 

Jokin. 




gabor

unread,
Mar 18, 2015, 3:17:59 AM3/18/15
to redbe...@googlegroups.com


Hi,

You could use an IN-clause like this:

foreach($books as $book) {
        $bookIDs
[] = $book->id;
}
$pages
= R::find('page', 'book_id IN ('.R::genSlots($bookIDs).')', $bookIDs);

But if all you do is send it to the client, why bother making beans in the first place?

cheers,
Gabor

gabor

unread,
Mar 18, 2015, 4:30:20 PM3/18/15
to redbe...@googlegroups.com


Might be interesting to add direct support for this.

Maybe something like?

R::findMulti('book,page', 'select from book ... join page ... etc ')

Jake Chapa

unread,
Mar 18, 2015, 4:52:58 PM3/18/15
to redbe...@googlegroups.com
That would be AWESOME

--
You received this message because you are subscribed to the Google Groups "redbeanphp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redbeanorm+...@googlegroups.com.
To post to this group, send email to redbe...@googlegroups.com.
Visit this group at http://groups.google.com/group/redbeanorm.
For more options, visit https://groups.google.com/d/optout.

gabor

unread,
Mar 21, 2015, 12:44:06 PM3/21/15
to redbe...@googlegroups.com


I'm gonna regret this but I implemented a 'Frankensteinian' function, but....  I think you'll like it.
It does not only do what you want, it also allows you to re-map these multi-mappings to native bean mappings (i.e. book->ownPages instead of just book,pages ).

https://github.com/gabordemooij/redbean/commit/c7069c1ddf494c3c3a80c0c57e5a7c48a87595f2


Example:

$stuff = $finder->findMulti('movie,review', ' SELECT movie.*, review.* FROM movie LEFT JOIN review ON review.movie_id = movie.id');

It might take a while to get used to it though.

cheers,
Gabor



Message has been deleted

gabor

unread,
Apr 9, 2015, 6:29:56 PM4/9/15
to redbe...@googlegroups.com

Hi,

If you are new to RedBeanPHP I recommend NOT to use findMulti, this is an advanced function.
Try to keep things simple:

$user->ownBook

should give you all the books of the user.

To add books to a user:

$user->ownBook[] = $book;

cheers,
Gabor


On Friday, April 10, 2015 at 12:24:06 AM UTC+2, Wil Liam wrote:
Hello Dear Gabor,
I'm beginner using Redbean and i have the same issue.
i have two tables and i want to retrieve all the book and user matching 
- book 
- user
My native sql is : 
SELECT * FROM book
INNER JOIN user
ON book.owner=user.id;

I tried this on redbean

$app->get('/book's, function () use ($app) { 
 // query database for all address
 $books= R::findMulti('book,user', ' SELECT book.*, user.* FROM book INNER JOIN user ON user.id = book.owner');
 // send response header for JSON content type
 $app->response()->header('Content-Type', 'application/json');
 
 // return JSON-encoded response body with query results
 echo json_encode(R::exportAll($books));
});

 And i got this error below: Please could you help me?

Type: ErrorException
Code: 4096
Message: Argument 1 passed to RedBeanPHP\DuplicationManager::dup() must be an instance of RedBeanPHP\OODBBean, array given, called in D:\wamp\www\project\vendor\RedBean\rb.php on line 12214 and defined
File: C:\wamp\www\parkavenue\vendor\RedBean\rb.php
Line: 12148
Reply all
Reply to author
Forward
0 new messages