Doctrine and unrefreshed relationships(教义和未更新的关系)
问题描述
我在 Doctrine (1.2.4) 中看到了意想不到的缓存效果.
I am seeing an unexpected caching effect in Doctrine (1.2.4).
我有几个由以下 YAML 定义的相关表(删除了示例中未使用的几个附加字段).只是从学生到学校的简单的一对多关系.
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
我可以创建一个简单的 Doctrine (1.2.4) 查询来找回一个学生
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();
然后用
foreach ($result1 as $result) { $ans[] = $result->School["name"]; }
我现在修改 school_id(导致关系),如下:
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(); }
(我已经设置了数据库,以便提供另一个有效的学校 ID).
(I have set up the DB so that this gives another valid school ID).
如果我现在要立即尝试访问该关系,我将获得旧学校的名称.我明白这一点 - 这是因为我没有调用 refreshRelated().我发现意外的是,如果我立即进行另一个查询,复制第一个查询
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();
得到结果
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 对关系使用一点缓存:你的 Student->School
被存储到一个 Student
属性,并且您的 Student->school_id
也在另一个属性中.
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.
当您更改 Student->school_id
时,数据库会被查询,并且 Student->school_id
会更改,但 Student->学校
不会,因为再水化这个对象可能会占用 CPU/内存.
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 提供 一些刷新关系的方法,但使用它是开发人员的责任.
Doctrine provide some method to refresh the relations, but it's the developper's responsability to use it.
例子:
$student->refreshRelated('School'); //refreshes only the School relation
$student->refreshRelated(); //refreshes every relation of the $student
但是还有另一个缓存.Doctrine 将所有水合对象保存在内存中,以限制请求数.因此,当您再次查询您的学生时,您会发现您的 Student->School
并没有改变.
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.
这篇关于教义和未更新的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:教义和未更新的关系
基础教程推荐
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01