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

Difficult to work with records that haven't been saved to database

It's not always necessary to save all ORM instances to database right away, for example when something needs to be previewed and could only be saved to database at a later time. The ORM seems to have some issues with this. The related records don't really work at all if they haven't been saved to database.

Example code:

$parent = new TheParent();
$parent->name = 'The parent';

$child = new Child();
$child->name = 'test';
$parent->children = [$child];

var_dump($parent->children->getFirst());

var_dump(count($parent->children));

var_dump($parent->children->toArray());

Output:

bool(false)
int(0)
array(0) {
}

Related thread with setup code: https://forum.phalcon.io/discussion/2007/two-orm-issues

Whatever way you look at it, it's simply not right.

Am I the only one that has come across this problem? I have been really lazy with unit testing lately, but it seems that this issue itself would prevent ORM from being testable (unless the tests involve storing all records to database). I really hope I'm missing something. Currently I'm getting around this issue by saving the records to database at the beginning of the request and deleting when calculations are finished (and gone are any performance gains...)