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..
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 ? 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..
// 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();
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.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 ?
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
It looks like you're new here. If you want to get involved, click one of these buttons!