Didier February 2016

Phalcon $model-find()->filter + paginate

Having a look in the doc, this code works :

$customers = Customers::find();

$numberPage = $this->request->getQuery("page", "int");
$paginator = new Paginator(array(
    "data"  => $customers,
    "limit" => 10,
    "page"  => $numberPage
));

But this one doesn't work, and I really don't understand why

$customers = Customers::find()->filter(
  function ($customer) {

    // Return only customers with a valid e-mail
    if (filter_var($customer->email, FILTER_VALIDATE_EMAIL)) {
        return $customer;
    }
  }
);

$numberPage = $this->request->getQuery("page", "int");
$paginator = new Paginator(array(
    "data"  => $customers,
    "limit" => 10,
    "page"  => $numberPage
));

Then, I get an "internal error". Strange, $customers object seems altered.

Any idea ?

By advance, thank you.

Answers


yergo February 2016

Result of Model::find() should be an instance of Phalcon\Mvc\Model\Resultset what is accepted by paginator of Phalcon\Paginator\Adapter\Model.

Result of Resultset::filter() method is an array of model instances, what is not an instance of Resultset. To paginate arrays there is Phalcon\Paginator\Adapter\NativeArray paginator implemented.

Post Status

Asked in February 2016
Viewed 1,952 times
Voted 11
Answered 1 times

Search




Leave an answer