加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 主义和没有新鲜的关系

发布时间:2020-12-13 18:06:31 所属栏目:PHP教程 来源:网络整理
导读:我在Doctrine(1.2.4)中看到了意想不到的缓存效果. 我有几个由以下YAML定义的相关表(删除了示例中未使用的其他几个字段).从学生到学校只是一个简单的1-Many关系. School: tableName: tblschool columns: sch_id: name: sch_id as id primary: true autoincrem
我在Doctrine(1.2.4)中看到了意想不到的缓存效果.

我有几个由以下YAML定义的相关表(删除了示例中未使用的其他几个字段).从学生到学校只是一个简单的1-Many关系.

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)查询来取回学生

$result1 = Doctrine_Query::create()
           ->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->save(); }

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

如果我现在,立即,尝试访问我将获得旧学校名称的关系.我理解这一点 – 因为我没有调用refreshRelated().我发现意外的是,如果我立即进行另一个查询重复第一个

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

并得到它的结果

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

当我检查我的数组的内容时,我发现,在这两种情况下,我都有相同的学校名称.换句话说,即使我已经完成了第二个查询并且查看了查询的结果,关系也不会刷新.

数据库中的数据很好且一致;即适当的学生和学校.例如.第二次运行上述序列 – 在不同的程序执行中 – 使用另一个学校名称(尽管再次重复).

这个缓存来自哪里?

Doctrine对关系使用一点缓存:您的Student-> School存储在Student属性中,而您的Student-> school_id也存储在另一个属性中.

当您更改Student-> school_id时,数据库会被查询,而Student-> school_id会更改,但Student-> School不会,因为重新润现此对象可能是CPU /内存扩展.

Doctrine提供了some method to refresh the relations,但开发人员使用它是负责任的.

示例:

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

但还有另一个缓存. Doctrine将所有水合物体保存在内存中,以限制请求数量.因此,当您再次查询您的学生时,您会发现您的学生 – >学校没有改变.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读