Search code examples
phpormredbean

One to many with RedBeanPhp ORM


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


Solution

  • 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).