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

Upgrading Phalcon 2 to 3 - difference in model relations

Hello everyone,, we're updating a large application from Phalcon 2 to 3. We've found that model relations behave differently. I can't find anything in the changelog and was hoping someone could help explain.

It seems that in Phalcon 2, relations are refreshed on each access. In Phalcon 3 the related object is stored and not refreshed. Is this correct?

Here is an example of sorts with a unit test, I can post more code if helpful:

// say we have a robot with 2 parts
$this->assertEquals(2, count($robot->Parts));

// add a new part without referencing the relation
$part = new Part();
$part->name = 'Arm';
$part->robotId = $robot->Id;
$part->save();

// the robot now has 3 parts
$this->assertEquals(3, count($robot->Parts));

In Phalcon 2 this works - $robot->Parts is correctly updated when I check the 2nd time. In Phalcon 3 the behaviour is different. The first access to robot->Parts caches the object and it is n longer refreshed.

I can't find this in the documentation on upgrading. Thanks!

edited Jun '19

Indeed it's correct, that's why i don't use any magic stuff that phalcon provides. Better refactor this and add method like getParts()

https://github.com/phalcon/cphalcon/blob/3.4.x/phalcon/mvc/model.zep#L4566 here is this related change

vs phalcon 2

https://github.com/phalcon/cphalcon/blob/phalcon-v2.0.13/phalcon/mvc/model.zep#L4214

Though it was assigned as property too which is kind of weird.