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

how to use query builder

hi all, I just learned phalcon, I'm trying to use query builder and get the following error message: Fatal error: Uncaught Error: Using $this when not in object context in D:\wamp\www\erp\app\models\WorkStaffsModel.php on line 10 . and code here:

<?php

use Phalcon\Mvc\Model\Query\Builder;

class WorkStaffsModel {

public static function getAll()
{
    $result = $this->modelsManager->createBuilder()
    ->from('work_staffs')
    ->getQuery()
    ->execute();

    return $result;
}

}

how to config query builder

edited Dec '18

As the interpreter says, you are trying to access an instance ($this->modelsManager) from a static context (public static function).

This is OOP 101, not exclusive to Phalcon....

What you want to do is get a reference to your DiInterface:

use Phalcon\Mvc\Model\Query\Builder;
use Phalcon\Di\FactoryDefault;

class WorkStaffsModel {

public static function getAll()
{
    $di = FactoryDefault::getDefault();
    $modelsManager = $di->get('modelsManager');
    $result = $modelsManager->createBuilder()
    ->from('work_staffs')
    ->getQuery()
    ->execute();

    return $result;
}
}

Thank you for your answer, then I encountered this error

Fatal error: Uncaught Error: Class 'ModelsManager' not found in D:\wamp\www\erp\lib\di.php on line 8