I noticed a bug when running to_array with nested sets with singular relations. It seems that if you have a has_one or belongs_to relation that is two or more relations deep in the model that you are converting to an array, the static $references variable never gets reset for the relations two or more levels deep. I would *think* that the $references variable would have only one instance, hence the static, but at least from the debug code I added, it looks like it has one instance per subclass of \Orm\Model, thus causing this issue.
Here's my debug output which should give a better idea. My data model is RECEIVABLE (belongs to) TENANT (belongs to) UNIT
DEBUG - 2013-08-24 21:34:02 --> Running to_array, references should be resetting...
DEBUG - 2013-08-24 21:34:02 --> Running to_array on model Model\Receivable. Recurse is
DEBUG - 2013-08-24 21:34:02 --> Checking for existence of Model\Tenant in the static references array, which is: array (
)
DEBUG - 2013-08-24 21:34:02 --> Running to_array on model Model\Tenant. Recurse is 1
DEBUG - 2013-08-24 21:34:02 --> Checking for existence of Model\Unit in the static references array, which is: array (
)
DEBUG - 2013-08-24 21:34:02 --> Running to_array on model Model\Unit. Recurse is 1
DEBUG - 2013-08-24 21:34:02 --> Running to_array, references should be resetting...
DEBUG - 2013-08-24 21:34:02 --> Running to_array on model Model\Receivable. Recurse is
DEBUG - 2013-08-24 21:34:02 --> Checking for existence of Model\Tenant in the static references array, which is: array (
)
DEBUG - 2013-08-24 21:34:02 --> Running to_array on model Model\Tenant. Recurse is 1
DEBUG - 2013-08-24 21:34:02 --> Checking for existence of Model\Unit in the static references array, which is: array (
0 => 'Model\\Unit',
)
As you can see, when it checks for a circular reference to the related tenant, the $references array is empty, as it should be. However then when it goes a level deeper and checks for a circular reference to the related unit, the $references array already has 'Model\Unit', and thus that unit is ignored and not put into the array.
What's weird is that I replaced the static $references variable with a static property on the \Orm\Model class, so everywhere in the to_array() method I replaced $references with static::$references. That fixed the issue. So it seems that there is some bug with PHP's static keyword within a method?
Anyway, the fix is very simple, but I wanted to make you aware of how I discovered it. Would it be worth it to submit a pull request for this or would it be easier for one of the devs to just change those few lines? Also, it's entirely possible that my PHP version is just out of whack and this is working fine on other machines, so I'd be interested to find that out.
class AAA { public function test() { static $var = 0;
$var++; echo "In class ",get_class($this),", variable is now: ", $var, "<br />"; } } class BBB extends AAA {} class CCC extends AAA {}
$a = new AAA; $b = new BBB; $c = new CCC; $a->test(); $b->test(); $c->test(); $a->test(); $b->test(); $c->test();
With as a result:
In class AAA, variable is now: 1 In class BBB, variable is now: 1 In class CCC, variable is now: 1 In class AAA, variable is now: 2 In class BBB, variable is now: 2 In class CCC, variable is now: 2
So this confirms your findings, the static is maintained per class.