We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

A strange problem: Undefined index: model?

I've got a problem in the Controllers:

2020/03/11 13:25:35 [error] 1333#1333: *2994 FastCGI sent in stderr: "PHP message: PHP Notice:  Undefined index: model in /var/www/.../app/controllers/Admin/ProblemsController.php on line 74
PHP message: PHP Stack trace:
PHP message: PHP   1. {main}() /var/www/.../public/index.php:0
PHP message: PHP   2. Phalcon\Mvc\Application->handle() /var/www/.../public/index.php:38
PHP message: PHP   3. Phalcon\Mvc\Dispatcher->dispatch() /var/www/.../public/index.php:38
PHP message: PHP   4. Phalcon\Mvc\Dispatcher->callActionMethod() /var/www/.../public/index.php:38
PHP message: PHP   5. ...\Controllers\Admin\ProblemsController->listAction() /var/www/.../public/index.php:38
PHP message: PHP   6. Phalcon\Paginator\Adapter\Model->paginate() /var/www/.../app/controllers/Admin/ProblemsController.php:74
PHP message: PHP Warning:  call_user_func() expects parameter 1 to be a valid callback, first array member is not a valid class name or object in /var/www/.../app/controllers/Admin/ProblemsController.php on line 74
PHP message: PHP Stack trace:
PHP message: PHP   1. {main}() /var/www/.../public/index.php:0
PHP message: PHP   2. Phalcon\Mvc\Application->handle() /var/www/.../public/index.php:38
PHP message: PHP   3. Phalcon\Mvc\Dispatcher->dispatch() /var/www/.../public/index.php:38
PHP message: PHP   4. Phalcon\Mvc\Dispatcher->callActionMethod() /var/www/.../public/index.php:38
PHP message: PHP   5. ...\Controllers\Admin\ProblemsController->listAction() /var/www/.../public/index.php:38
PHP message: PHP   6. Phalcon\Paginator\Adapter\Model->paginate() /var/www/.../app/controllers/Admin/ProblemsController.php:74
PHP message: PHP   7. call_user_func:{/var/www/.../app/controllers/Admin/ProblemsController.php:74}() /var/www/.../app/controllers/Admin/ProblemsController.php:74" while reading response header from upstream, client: 10.0.10.171, server: default_..., request: "GET /admin/problems/list HTTP/1.1", upstream: "fastcgi://unix:

the code of /ProblemsController.php arround line 74:

use Phalcon\Paginator\Adapter\Model as Paginator;
...
public function listAction()
{

    $numberPage = $this->request->getQuery("page", "int", 1);

    $problems = Problems::find(array(
                "active = :active:",
                "bind" => ["active" => $active],
                "order" => "id DESC"
            )); 

    $paginator = new Paginator(array(
        'data' => $problems,
        'limit' => 1000,
        'page' => $numberPage
    ));

    $this->view->page = $paginator->paginate();  // this line is line 74
}


125.7k
Accepted
answer
edited Mar '20

You're not defining the 'model' attribute: https://docs.phalcon.io/4.0/en/pagination#model

Basically, rather than running a find() ahead of time, you pass the class name and find parameters to the Paginator.



31.3k
edited Mar '20

Thanks!

Under Phalcon 3.2 I just doubt on the performance of the paging after find(), and this new way satisfy all the thing.

BTW: in version 3.2, the result of $page->items is an object, and in new version is an array. All of my old code is "object-like" item->title, item->id, I must change all the code to "array-like"item['title'],item['id']. Is there a method that can get and object of $page->item?

edited Mar '20

If it's truly an array, you could just cast it to an object.

foreach($page->items as $a_item){
    $item = (object)$a_item;
    $item->id

I don't see anywhere where you can set a hydration mode for the structure of items. But... that doesn't necessarily mean it doesn't exist.

There is a helper for casting an array to object.

edited Apr '20

You're not defining the 'model' attribute: https://docs.phalcon.io/4.0/en/pagination#model walgreenslistens Basically, rather than running a find() ahead of time, you pass the class name and find parameters to the Paginator.

Very Helpful Post .......!! thanks for sharing with us.

edited Apr '20

I must modify too much code , if i update phalcon from 3.4 to 4.0

this is a very very very stupid.

You Should Restore this.

phalcon3.4 don't support json

if you must use json field , you must update 4.0

but ,your project 's paginator boom

you must restore old Phalcon\Paginator\Adapter\Model adapter.

and add a new version Phalcon\Paginator\Adapter\Model adapter.