How to stop Doctrine 2 from caching a result in Symfony 2?(如何阻止 Doctrine 2 在 Symfony 2 中缓存结果?)
问题描述
我希望能够检索实体的现有版本,以便将其与最新版本进行比较.例如.编辑一个文件,我想知道自从在数据库中后该值是否发生了变化.
I want to be able to retrieve the existing version of an entity so I can compare it with the latest version. E.g. Editing a file, I want to know if the value has changed since being in the DB.
$entityManager = $this->get('doctrine')->getEntityManager();
$postManager = $this->get('synth_knowledge_share.manager');
$repository = $entityManager->getRepository('KnowledgeShareBundle:Post');
$post = $repository->findOneById(1);
var_dump($post->getTitle()); // This would output "My Title"
$post->setTitle("Unpersisted new title");
$existingPost = $repository->findOneById(1); // Retrieve the old entity
var_dump($existingPost->getTitle()); // This would output "Unpersisted new title" instead of the expected "My Title"
有谁知道我如何解决这个缓存问题?
Does anyone know how I can get around this caching?
推荐答案
这是正常现象.
Doctrine 将检索到的实体的引用存储在 EntityManager 中,因此它可以通过其 id 返回实体,而无需执行其他查询.
Doctrine stores a reference of the retrieved entities in the EntityManager so it can return an entity by it's id without performing another query.
你可以这样做:
$entityManager = $this->get('doctrine')->getEntityManager();
$repository = $entityManager->getRepository('KnowledgeShareBundle:Post');
$post = $repository->find(1);
$entityManager->detach($post);
// as the previously loaded post was detached, it loads a new one
$existingPost = $repository->find(1);
但请注意,由于 $post 实体已分离,如果您想再次持久化它,则必须使用 ->merge() 方法.
But be aware of that as the $post entity was detached, you must use the ->merge() method if you want to persist it again.
这篇关于如何阻止 Doctrine 2 在 Symfony 2 中缓存结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何阻止 Doctrine 2 在 Symfony 2 中缓存结果?


- PHP - if 语句中的倒序 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01