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

ModelManager::getRelations doesn't work even on initialized models

Hi,

I'm using \Phalcon\Mvc\Model\Manager in my Unit-Tests to check if all relations between the models are set correctly. For that, I've placed the ModelManager inside my DI:

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

and retrieve it again in my tests:

    $modelsManager = $this->di->get('modelsManager');

Now I'm trying to run through all model-relations of my model "Attributes" with $modelsManager->getRelations('Attributes'), but I only retrieve an empty array, even though I have several "belongsTo"-relations set in the initialize-method of the model.

I have tried to check if the model is initialized in the ModemManager using "isInitialized" (which returns "false"), then I used $modelsManager->initialize( new \Attributes()); and tried again, this time "isInitialized" returns "true", but I keep getting an empty array, when trying to get the relations.

Now, the funny thing is, that I retrieve the relations correctly, when I call another method in the ModelManager beforehand:

    $modelsManager->existsBelongsTo('Attributes', 'Objects');

This returns "true" and afterwards calling "getRelations()" will return the correct array with all relations!

Any hint on what I'm missing is appreciated.

Kind regards, Dirk



98.9k

Try setting the service as shared:

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

Hi,

thanks for your quick answer! Unfortunately, setting the service as shared doesn't change anything.

Kind regards, Dirk