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 phql in Models

      >    class Users extends Model{
      >    public $id;
      >      public $name;
      >      public $insment;
      >     ...
      >  public static function changeInstallment($ins,$userid){
      >         $query = new Query("UPDATE Users SET insment = ".$ins."  WHERE id = ".$userid);
      >        $result = $query->execute();
      >         return $result->success();
      >     }

when the error is : Trying to call method load on a non-object

edited Nov '15

before return line


All right. To use PHQL on Model, use this:

public static function changeInstallment($ins,$userid){
    $di = \Phalcon\DI::getDefault();
    $query = "UPDATE Users SET insment = ".$ins."  WHERE id = ".$userid;

    $query = new \Phalcon\Mvc\Model\Query($query, $di);

    return $query->execute();

I hope this helps.


thanks but when I use your answer also has error Fatal error: Using $this when not in object context in E:\Desktop\front\app\models\Users.php on line 46 but I accept your answer because I mistake click


thank for your answer , yours is right , but I have a mistake to click accept answer, in your answer I will new a DI every time , it's will be make app slow ?

I've deleted my answer. You can choose hvjohny answer :)

$this in my example is allowed only in controller but you put them in the static method in Model which is not allowed.

No. Calling $di = \Phalcon\DI::getDefault() won't affect your performance.


thank you for your reply, It dosent matter to deleted yours answer. and thank you your DI reply.