问题
I ran in to a problem what i never experienced with fuelphp.
On the users profile page i allow other users to leave comments.
And when i use related comments limit
in my controller function its just ignored.
Code
public function action_view($id)
{
$user = Model_User::find($id, array(
'related' => array(
'comments' => array(
'order_by' => array(
array('id', 'DESC'),
),
),
),
'limit' => 5,
));
if(empty($user)):
Response::redirect(Uri::base() . "welcome/404");
endif;
$this->template->title = $user->username . "'s Profile | " . Config::get('site_name');
$this->template->content = View::forge('user/profile', array('user' => $user));
}
The order_by
works like a charm, but the limit doesnt, it still lists all the comments.
Tryed multiple variations got lot of different weird errors.
回答1:
EDIT: I've found this part of the docs, it should help you: troubleshooting
Try to put the "limit" in the properties array: in the current position the "limit" limit the number of user, but you are retrieving a single user so it doesn't work.
Try with this:
$user = Model_User::find($id, array(
'related' => array(
'comments' => array(
'order_by' => array(
array('id', 'DESC'),
),
'limit' => 5,
),
),
));
回答2:
The issue here is that this query runs a find by value. This type of find runs a get_one(), and is always limited by 1.
So your limit isn't ignored, it's overwritten by the fact that you use this type of find().
To do what you want:
$user = Model_User::query()
->where('id', '=', $id)
->related('comments')
->order_by('comments.id', 'DESC')
->rows_limit(5)
->get();
来源:https://stackoverflow.com/questions/13399884/fuelphp-orm-related-limit-ignored