What is the scope of a Doctrine Record?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


What is the scope of a Doctrine Record?



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.


HYDRATE_ARRAY


$thing1


$thing2



I would very much like to know why this happens and why the behavior is desirable.





You've created a rather complex problem for yourself. Your example code is not complete however, it does nothing. Could you complete it, show us what output you get, and what output you expected? What I am asking is to create a Minimal, Complete, and Verifiable example.
– KIKO Software
yesterday







@KIKOSoftware sorry for the lack of completeness. I am having a hard time thinking of how to create it more complete, short of spinning up a repo with a light weight app and bootstrap database scripts. Could you point me to any examples of questions that are backed by a SQL database?
– moe
yesterday





Let me put it this way: I can see where the string "new value" comes from, but I have no idea where "old value" comes from? Is it from the Doctrine query? In what order are the methods of the class called? A tip: I don't think this has anything to do with the database call, so you can make your example complete when you replace that call by: return "value set in getById($id)";.
– KIKO Software
yesterday


"new value"


"old value"


return "value set in getById($id)";









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS