I am investigating a bug where it looks like Doctrine does not respect the idea of scope on variables when returning Record objects.
<?php
class Thing {
var $variable;
public function doStuff() {
$thing1 = self::getById(1);
echo($thing1->variable);
$thing1->variable = "new value";
Thing::doSomeOtherStuff();
echo($thing1->variable);
}
public static function doSomeOtherStuff() {
$thing2 = Thing::getById(1);
}
/**
* this runs a query against a database and
* returns a Thing object with $variable = "old value"
*/
public static function getById($id): Thing {
return Doctrine_Query::CREATE()
->select('t.*')
->from('Thing t')
->where('t.id = ?', $id)
->fetchOne([], Doctrine_Core::HYDRATE_RECORD);
}
}
expected output:
old value
new value
actual output:
old value
old value
The the odd behavior does not happen when using HYDRATE_ARRAY
. Using a debugger it looks like this method is doing the actual updating of the value of $thing1
while creating $thing2
.
I would very much like to know why this happens and why the behavior is desirable.