There are 2 issues with ORM that have been bothering me for a while and I'm wondering if there's any solution.
1) toArray() method does not always set the ID's of the related records. Here's the entire code to reproduce the problem:
<?php
$di = new Phalcon\DI\FactoryDefault();
$di->set('db', function() {
return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => 'localhost',
"username" => 'root',
"password" => '',
"dbname" => 'testing'
));
});
class TheParent extends \Phalcon\Mvc\Model
{
public function initialize()
{
$this->hasMany('ID', 'Child', 'parentID', array('alias' => 'Children'));
}
}
class Child extends \Phalcon\Mvc\Model
{
public function initialize()
{
$this->belongsTo('parentID', 'TheParent', 'ID', array('alias' => 'Parent'));
}
}
$parent = new TheParent();
$parent->name = 'The parent';
$parent->save();
$child = new Child();
$child->parent = $parent;
var_dump('Parent', $parent->toArray());
var_dump('Child', $child->toArray());
Output:
string(6) "Parent"
array(2) {
["ID"]=>
string(1) "9"
["name"]=>
string(10) "The parent"
}
string(5) "Child"
array(3) {
["ID"]=>
NULL
["parentID"]=>
NULL
["name"]=>
NULL
}
Expected output for the second var_dump would be:
string(5) "Child"
array(3) {
["ID"]=>
NULL
["parentID"]=>
string(1) "9"
["name"]=>
NULL
}
Any reason why the parentID field would remain NULL?