How to stop Doctrine 2 from caching a result in Symfony 2?

后端 未结 2 1156
礼貌的吻别
礼貌的吻别 2020-12-30 21:17

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 sin

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-30 21:48

    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.

提交回复
热议问题