2015-11-20 10:22

我可以强制Doctrine 2存储库首先检查entityManager的结果

  • php
  • persistence
  • entity

I expected that Doctrine 2 repository would check the EntityManager for results first. But it doesn't.

This code represents the issue.

$em = $this->getEntityManager();

$firstName = 'Michael';
$lastName = 'Jordan';
$userEntity = new User($firstName, $lastName);


$userRepository = $em->getRepository('UserRepository');

$expectingMichaelJordanUserEntity = $userRepository->findOneBy(array('firstName' => $firstName, 'lastName' => $lastName));

In this example the $expectingMichaelJordanUserEntity is null because the entity has not been saved to the database. Shouldn't it look inside the entity manager for persisted entities first?

Is that the meaning or is there a way to get my example to return the persisted User entity?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • doudihuang7642 doudihuang7642 6年前

    You are asking doctrine to findOneBy those criteria inside the database. In your case it returns null but it could as well return a record if there would be such a user firstName = 'Michael' AND lastName = 'Jordan' inside your database.

    The result of this query has nothing to do with your newly created user.

    If it would return the user from the EntityManager then you would not know whether it is from the database or from memory.

    点赞 评论 复制链接分享