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

More documentation for Models in namespace

Hi, just checking the "Working with models" section of the documentaiton and i think a fewer examples about how to use namespaced models would be welcome. Maybe it is located everywhere ? But didnt find it.

Thanks dev team !



98.9k

Just define the class into a namespace:

namespace Store\Models;

class Robots extends Phalcon\Mvc\Model
{

}

If you have relationships they must include the namespace too:

namespace Store\Models;

class Robots extends Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->hasMany('id', 'Store\Models\Parts', 'robots_id', array(
            'alias' => 'parts'
        ));
    }   
}

In PHQL you must write the statements including namespaces:

$phql = 'SELECT r.* FROM Store\Models\Robots r JOIN Store\Models\Parts p';

You must use an autoloader that supports a namespace strategy: https://docs.phalcon.io/en/latest/reference/loader.html#registering-namespaces

Thanks for that,

moreover as i said here in the last reply : https://forum.phalcon.io/discussion/434/namespaces-in-model-generation and here in the comment : https://github.com/phalcon/phalcon-devtools/commit/547d35af4940375e891911b8b0283779c497664d#commitcomment-3446388

I think a little mistake was done in the dev tools. Just have a look.