Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM relation conditions ?
  • Hi guys, I currently work with Fuel, and I've a problem.. I have a blog, and I get the post with :
    Model_Post::find_by_slug($slug);
    
    i would like to get all comments. So I've create a relationship in the Post Model :
    protected static $_has_many = array('comments' => array(
      'model_to' => 'Model_Comment',
      'key_from' => 'id',
      'key_to'   => 'post_id',
      'cascade_save' => true,
      'cascade_delete' => false,
     ));
    
    I've update the request in my controller :
    Model_Post::find_by_slug($slug, array('related' => 'comments'));
    
    For the first post, it works, but not for others..
    If I leave
    Model_Post::find_by_slug($slug);
    
    it works for all, with comments... My question : Why it works if I do not specify the 'related' arg ? How to make it work everywhere? And how to make a request without comments ? :s :s Subsequently, i would like to get comments in 'order_by'.. With
    Model_Post::find_by_slug($slug, array('related' => array('comments' => array('order_by' => array('created_at' => 'desc')))));
    
    it works, but just for the first.. I obviously read the documentation before to post..
    And sorry, i'm a n00b in English ^^
  • Are you using 1.1 final? The 1.1-RC1 version had a bug that caused exactly this behavior. You can echo Fuel::VERSION to know for sure which version you're using.
  • With an echo of Fuel::Version, I've just 1.1.. It's the final version ? To be sure, I've re-downloaded the archive on the official website, and I've replaced the core and package folder.. I've still same version, and still same problems.. :/
  • Could you try the following (it's the method chaining equivalent of the code you posted):
    // instead of Model_Post::find_by_slug($slug, array('related' => 'comments'));
    Model_Post::query()->where('slug', $slug)->related('comments')->get_one();
    
    // Model_Post::find_by_slug($slug, array('related' => array('comments' => array('order_by' => array('created_at' => 'desc')))));
    Model_Post::query()->where('slug', $slug)->related('comments')->order_by('comments.created_at', 'desc')->get_one();
    
  • Thanks for reply ;) it works perfectly.
    But.. How do I get just the post ? With Model_Post::find_by_slug($slug), the relationship is automatically activated, and the comments are retrieved.. if it helps, ControllerPosts & ModelPost ...
  • Damn, that means there's a bug somewhere in the logic translating the find_by_... stuff into method chaining :(. They won't (/shouldn't) be auto-fetched unless you actively request them. The ORM does support lazy loading which means you don't have to include the relations when fetching the base model's instances. But once you do request the relation they're automatically fetched from the DB. If you're requesting the related property somewhere they'll always be fetched, either during your initial load (eager-loaded) or at that point in time (lazy-loaded).
  • I'm referring to the relationship in that part of code, not elsewhere.. :/
    I'll re-download Fuel, and restart the project, see if I have still the same beug .. or whether it's because I'm in localhost .. An other small question : if I define an 'has_many' relationship in Model_Post, I've to define a 'belongs_to' relationship in Model_Comment ?
  • An other small question : if I define an 'has_many' relationship in Model_Post, I've to define a 'belongs_to' relationship in Model_Comment ?
    You don't have to define it unless you intend to use it, but if you won't ever use it in the other direction there's no reason to define it. I always do however as there's pretty much always a reason that might come up in the future.
  • Thanks for help ;)
    I hope to master fuel quickly, it looks really promising :)
  • i can confirm this bug as well
    Model_Some::find_by_slug($slug, array('related' => 'something'))
    
    returns null if there are no something rows
    Model_Some::query()->where('slug' => $slug)->related('something')->get_one()
    
    returns the object even if there are no something rows
  • I just came across this exact bug. Had me confused for a while till I tried doing the whole find_by_slug without the related array.

Howdy, Stranger!

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

In this Discussion