Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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
var_dump($result);

before return line



1.4k

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.



13.0k

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



13.0k

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.



13.0k

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