Fuelphp ORM related limit ignored

徘徊边缘 提交于 2019-12-12 03:38:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!