Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Pagination problems, non-object errors
  • I am wanting to user pagination when a user searches for some data based on the input that they have given, however I keep on hitting the "Call to a member function ****() on a non-object" and I don't understand why. Here is my code for the pagination in my controller: 

         $data['properties'] = Model_Property::find('all', array('where' => array(array('bedrooms', '<=', Input::post('highbed')),
    array('bedrooms', '>=', Input::post('lowbed')),
    array('price', '<=', Input::post('highprice')),
    array('price', '>=', Input::post('lowprice')))));
    $count = count($data['properties']);
    $config = array(
    'total_items'    => $count,
    'per_page'       => 1,
    'uri_segment'    => 3,
    }
    $pagination = Pagination::forge('mypagination', $config);

    $data['properties'] = Model_Property::find('all', array('where' => array(array('bedrooms', '<=', Input::post('highbed')),
    array('bedrooms', '>=', Input::post('lowbed')),
    array('price', '<=', Input::post('highprice')),
    array('price', '>=', Input::post('lowprice'))))
    ->limit($pagination->per_page)
                                                           ->offset($pagination->off_set)->get();

    $data['pagination'] = $pagination->render();
    $view = View::forge('properties/list', $data);
    $this->template->content = $view;

    And in my view:

    <?php echo $pagination->render() ?>

    I get the error with limit(), offset(), I then set these to:

    array('price', '>=', Input::post('lowprice')))),$pagination->per_page,$pagination->off_set)->get();

    and the errors go, but then I get them for get() which I then get rid of and then finally for render(). I only went through the documentation for the ORM and it doesn't work, where am I going wrong?
  • There is no property "off_set", so you should have had at least a notice error. I suggest you enable all error reporting, so you see all errors.

    You don't say what method call gives the error, nor the line and file that this call is on, so that leaves me with a lot of guess work.

    All I can see in this snippet is:
    - you render the pagination in your controller, and again in your view (which is not going to work)
    - you run the same code and same database query twice
    - and you mix static usage (the find method) and method chaining, which doesn't work

    It is more efficient to prep the ORM object using method chaining, and run a count() on it:

    $query = Model_Property::query()
        ->where('bedrooms', '<=', Input::post('highbed'))
        ->where('bedrooms', '>=', Input::post('lowbed'))
        ->where('price', '<=', Input::post('highprice'))
        ->where('price', '>=', Input::post('lowprice'));

    $config = array(
         'pagination_url' => 'http://localhost/fuel/posts/index/',
         'total_items'    => $query->count(),
         'per_page'       => 1,
         'uri_segment'    => 3,
    );

    $data['pagination'] = Pagination::forge('mypagination', $config);
    $data['properties'] = $query
        ->limit($data['pagination']->per_page)
        ->offset($data['pagination']->offset)
        ->get();

  • Also note that you are using input data without any validation, which from a security point of view is a bad idea...

Howdy, Stranger!

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

In this Discussion