如何停止教条2缓解结果在Symfony 2? [英] How to stop Doctrine 2 from caching a result in Symfony 2?
问题描述
我希望能够检索一个实体的现有版本,以便将其与最新版本进行比较。例如。编辑一个文件,我想知道这个值是否在数据库中改变了。
$ entityManager = $ this-> ;获得( '教义') - > getEntityManager();
$ postManager = $ this-> get('synth_knowledge_share.manager');
$ repository = $ entityManager-> getRepository('KnowledgeShareBundle:Post');
$ post = $ repository-> findOneById(1);
var_dump($ post-> getTitle()); //这将输出我的标题
$ post-> setTitle(Unpersisted new title);
$ existingPost = $ repository-> findOneById(1); //检索旧实体
var_dump($ existingPost-> getTitle()); //这将输出Unpersisted new title而不是预期的我的标题
有谁知道如何解决这个缓存?
这是一个正常的行为。
/ p>
$ entityManager = $ this-> get('doctrine') - > getEntityManager();
$ repository = $ entityManager-> getRepository('KnowledgeShareBundle:Post');
$ post = $ repository-> find(1);
$ entityManager-> detach($ post);
//因为先前加载的帖子被分离,它加载一个新的
$ existingPost = $ repository-> find(1);
但是请注意,由于$ post实体已分离,您必须使用 - > merge )方法,如果你想再次坚持。
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?
It's a normal behavior.
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.
You can do something like :
$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);
But be aware of that as the $post entity was detached, you must use the ->merge() method if you want to persist it again.
这篇关于如何停止教条2缓解结果在Symfony 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!