Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM related limit ignored
  • 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.

    So would someone give me a hint please?

    thank you
  • This has been asked so many times.

    See http://fuelphp.com/docs/packages/orm/intro.html#/troubleshooting for the explaination.
  • I took a look there too, i tried with rows_limit, it ignores that either
  • HarroHarro
    Accepted Answer
    I can asure you, both limit and rows_limit work, I use them daily in my applications.

    I think it has to do with your understanding of the way find() works.

    You're passing $id to the find() which either returns 1 record if the $id matches a primary key (a PK query is always LIMIT 1), or it doesn't do anything in case $id == null.

    And as the documentation says, the LIMIT 1 with relations means you get the requested record, plus ALL related records. It's the way the ORM is designed.

    You can work around it by doing a find('all'), and add a where clause of the $id. This does not insert the LIMIT 1, but uses the limit specified in the array.
  • Thank you for your help Harro

Howdy, Stranger!

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

In this Discussion