Doctrine and unresolved relationships

I see an unexpected caching effect in Doctrine (1.2.4).

I have a couple of related tables defined by the following YAML (a few extra fields that are not used in the example are deleted). Just a simple β€œ1-Many” relationship from students to schools.

School: tableName: tblschool columns: sch_id: name: sch_id as id primary: true autoincrement: true type: integer(4) sch_name: name: sch_name as name type: string(50) Student: tableName: tblstudent columns: stu_id: name: stu_id as id primary: true autoincrement: true type: integer(4) stu_sch_id: name: stu_sch_id as school_id type: integer(4) relations: School: local: school_id foreign: id foreignAlias: Students 

I can create a simple Doctrine query (1.2.4) to get the student back using

  $result1 = Doctrine_Query::create() ->from('Student s') ->where('s.id = 1') ->execute(); 

and then cross out the appropriate school name with

 foreach ($result1 as $result) { $ans[] = $result->School["name"]; } 

Now I am changing school_id (which calls the connection), following this with:

 foreach ($result1 as $result) { $result["school_id"] = 1 - $result["school_id"]; $result->save(); } 

(I created a database so that it gives another valid school identifier).

If I were now, immediately, try to access the relationship, I will get the old name of the school. I understand this - this is because I did not call refreshRelated (). What I find unexpected is that if I immediately make another request duplicating the first

  $result2 = Doctrine_Query::create() ->from('Student s') ->where('s.id = 1') ->execute(); 

and get his result

 foreach ($result2 as $result) { $ans[] = $result->School["name"]; } 

when I look at the contents of my array, I find that in both cases I have the same school name. In other words, although I made a second request and look at the result of the request, the connection is not updated.

The data in the database is accurate and consistent; those. there are relevant students and schools. For example. performing the above sequence a second time β€” when executing another program β€” uses a different school name (although it is duplicated again).

Where does this caching come from?

+7
source share
1 answer

Doctrine uses a little caching for relationships: your Student->School is stored in the Student attribute, and your Student->school_id also in a different attribute.

When you change your Student->school_id , a database query is requested, and Student->school_id changed, and Student->School is not, since rehydration of this object can be an extension of CPU / memory.

Doctrine provides some method for updating relationships , but it is responsible for developing to use it.

Example:

 $student->refreshRelated('School'); //refreshes only the School relation $student->refreshRelated(); //refreshes every relation of the $student 

But there is one more caching. Doctrine stores all hydrated objects in memory to limit the request number. Therefore, when you again request your student, you will find that your Student->School not changed.

+11
source

All Articles