学说和无关系的关系 [英] Doctrine and unrefreshed relationships

查看:96
本文介绍了学说和无关系的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Doctrine(1.2.4)中看到一个意想不到的缓存效果。



我有几个由以下YAML定义的相关表用在示例中删除)。只是一个简单的从学生到学校的1-Many关系。

 学校:
tableName:tblschool
列:
sch_id:
name:sch_id as id
primary:true
autoincrement:true
类型:integer(4)
sch_name:
name:sch_name as name
type:string(50)
学生:
tableName:tblstudent
列:
stu_id:
名称:stu_id as id
primary:true
autoincrement:true
类型:整数(4)
stu_sch_id:
名称:stu_sch_id as school_id
类型:整数(4)
关系:
学校:
本地:school_id
外国人:id
foreignAlias:学生

我可以使用



创建一个简单的Doctrine(1.2.4)查询来取回学生$ pre> - > from('Student s')
- > where('s.id = 1')
- > execute();

,然后用



提取相应的学校名称

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

我现在通过以下方式修改school_id(导致关系):

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

(我已经设置了数据库,这样可以获得另一个有效的学校ID)。 >

如果我现在立即,尝试访问关系,我会得到老学校的名字。我明白了 - 这是因为我没有调用refreshRelated()。我发现意外的是,如果我立即再次查询重复第一个

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

并获得结果

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

当我检查我的数组的内容时,我发现,我有同样的学校名称。换句话说,即使我做了第二个查询,并且查看查询的结果,关系也没有刷新。



数据库中的数据很好一致;那就是适当的学生和学校。例如。在另一个程序执行中再次执行上述顺序 - 使用其他学校名称(尽管再次重复)。



这个缓存来自哪里? >

解决方案

学说对关系使用一点缓存:您的学生 - >学校存储在学生属性中,您的 Student-> school_id 也在另一个属性中。



当您更改 Student-> school_id 时,数据库将被查询,而 Student-> ; school_id 更改,但 Student-> School 不会,因为这个对象可能是CPU /内存扩展的复水。

Doctrine提供一些刷新关系的方法,但是开发者的责任是使用它。



示例:

  $ student-> refreshRelated('School ); //刷新学校关系
$ student-> refreshRelated(); //刷新$ student

的每个关系,但还有一个缓存。教义将所有水合物保持在记忆中,以限制请求号。所以当你再次询问你的学生时,你发现你的 Student-> School 没有改变。


I am seeing an unexpected caching effect in Doctrine (1.2.4).

I have a couple of related tables defined by the following YAML (several additional fields not used in the example removed). 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 (1.2.4) query to get back a student with

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

and then extract out the corresponding school name with

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

I now modify the school_id (that causes the relationship) by following this with:

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

(I have set up the DB so that this gives another valid school ID).

If I were to now, immediately, try to access the relationship I will get the old school's name. I understand this - it is because I've not called refreshRelated(). What I find unexpected is that if I immediately make another query duplicating the first

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

and get its result

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

when I examine the contents of my array I find that, in both cases, I have the same school name. In other words, even though I've done a second query and am looking at the result of the query the relationship is not refreshed.

The data in the database is fine and consistent; i.e. appropriate students and schools exist. E.g. running the above sequence a second time - in a different program execution - uses the other school name (although again duplicated).

Where is this caching coming from?

解决方案

Doctrine use a little caching on the relations : your Student->School is stored into a Student attribute, and your Student->school_id too, in another attribute.

When you change your Student->school_id, the database get queryied, and the Student->school_id change, but the Student->School doesn't, as rehydrating this object might be CPU/memory expansive.

Doctrine provide some method to refresh the relations, but it's the developper's responsability to use it.

Example :

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

But there's another caching. Doctrine keeps all the hydrated object in memory, in order to limit the request number. So when you query again for your student, you find that your Student->School hasn't changed.

这篇关于学说和无关系的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆