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

Getting related objects

I have 2 entites: ActivityParameter and Activity and some logic:

Activity entity has relationship:

    $this->hasMany('id', 'ActivityParameter', 'activity_id', [

        'alias' => 'parameters',
        'reusable' => true,
        'foreignKey' => [
            'action' => Relation::ACTION_CASCADE
        ]
    ]);

Logic:

    $parameter = new ActivityParameter();
    $parameter->setValue(10.101);
    $parameter->setUnitId(1);

    $activity = new Activity();
    $activity->parameters = $parameter;

My problem is that:

count($activity->parameters);

gives 1, but:

count($activity->getParameters());

gives 0.

How to resolve this problem? I need to use getParameters() beacause later I use:

$activity->getParameters("value = '10.101'");

I found it is needed to make $activity->save(); to use $activity->getParameters(). I don't want to save these objectd because it's unit test. How to fix it?

You have to create your own getParameters method, beacause by default its(i guess) accessing getRelated method which is getting data from database.

Just create your own getParameters which will actually return parameters. Or just save before.