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 model of the access method modelsManager->createQuery

Notice: Access to undefined property Signup::modelsManager

class Signup extends \Phalcon\Mvc\Model { public function busy_checking($username, $email) {

    $query = $this->modelsManager->createQuery("SELECT * FROM Users WHERE username = :username: OR email = :email:");

    $users = $query->execute(array(
        'username' => $username,
        'email' => $email
    ));

    return count($users);
}


98.9k

"modelsManager" is not a property of Phalcon\Mvc\Model



11.9k

and then how can I use it from the model?



98.9k
Accepted
answer

This is the way:

<?php

class Signup extends \Phalcon\Mvc\Model
{

    public function busy_checking($username, $email)
    {
        $phql = "SELECT * FROM Users WHERE username = :username: OR email = :email:";
        return count($this->getModelsManager()->executeQuery($phql, array(
            'username' => $username,
            'email' => $email
        )));        
    }

}

However, there is a short way:

class Signup extends \Phalcon\Mvc\Model
{

    public function busy_checking($username, $email)
    {
        return Users::count(array(
            "username = :username: OR email = :email:",
            'bind' => array('username' => $username, 'email' => $email)     
        ));
    }

}