Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One to many with RedBeanPhp ORM

Tags:

php

orm

redbean

I would like to retrieve some records of a linked table :

table "portfolio" :
-id
-title

table "portfolio_img" :
-id
-image
-id_portfolio

The {id_portfolio} field is the foreign key of the "portfolio" table : {id} field.

How can I get all the "portfolio_img" records using an {id_portfolio} field (not using the classic way of R::find(), of course ;) ) ?

Regards

like image 949
fabrice Avatar asked Jul 21 '26 21:07

fabrice


1 Answers

The way redbean is designed, you would need to rename the field to portfolio_id. Then you would be able to access all the images by calling the portfolio bean and the own attribute.

$portfolio=R::load('portfolio',1);
echo $portfolio->title;
foreach($portfolio->ownPortfolio_img as $img){
    echo $img->image;
}

Now you can add an image as well, using:

$image=R::dispense("portfolio_img");
$image->image="myimage.jpg";
$image->portfolio=R::load('portfolio',1);
R::store($image);

I do a similar thing in my scripts (one to many - Company to Contacts).

like image 186
Tim Withers Avatar answered Jul 23 '26 12:07

Tim Withers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!