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 get model relations

i'm trying to get all relations from a model using this method:

$di->set('modelsManager', function() {
      return new Phalcon\Mvc\Model\Manager();

On controller I've tried these variants and all of them are giving me an empty array.


In the documentation API the method looks like this:

public Phalcon\Mvc\Model\RelationInterface [] getRelations (string $modelName)

My model have this structure name: /ns1/ns2/Model

Is this a bug or I don't know how to use this method?

I found an alternative solution that works but is not beautiful:

$array1 = $this->modelsManager->getBelongsTo(new  \ns1\ns2\Model()); 
$array2 = $this->modelsManager->getHasOneAndHasMany(new  \ns1\ns2\Model());
array_merge($array1, $array2);


There's no need to set up modelsManager specifically (as long as you are using \Phalcon\DI\FactoryDefault, cause its already loaded). Regarding your question - no, its not a bug. First of all, you're using wrong slashes (yep, these \ are correct). And most importantly - you first need to load/initialize model in modelsManager:

/** or */
$this->modelsManager->initialize(new User());
/** and now you can get them */
$relations = $this->modelsManager->getRelations('User');


many thanks, it's working now