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);
$em->persist($userEntity);
$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?
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.