Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Get some ORM objects with relation if...
  • Hi 

    I just want to know something : For the example, let's assume a picture gallery with two table, gallery and picture, gallery has_many pictures, picture belongs_to gallery. Picture can be set in gallery cover with a field 'cover', null or true, right ? 

    So if i want select all my galleries, and the cover image if the gallery have one, i naturally do : 

    $galleries = Model_Gallery::query()
                         ->related('images', array('where' => array(array('images.cover', '=', 1))))

    Aaand... no. The galleries who don't own a cover picture aren't selected at all. I checked the query and logically, there is a where at the end who exclude them.

    I just want to not select relating picture if there is no cover in gallery but select the gallery in each case.  

    So i have a way to do it or it's not possible with the ORM ?

  • I would do that with two relations:
    - gallery has_many pictures
    - gallery has_one coverpicture

    both pointing to the same Model_Picture, and add a picture_id to gallery as FK for the cover picture.

    Assuming your controller logic makes sure a cover picture can only be selected from the pictures in the gallery, both can share the same belongs_to the other way (picture->gallery).

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion