mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
DDC-1041: UnitOfWork tryGetById method is always called with the rootEntityName #1296
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @doctrinebot on GitHub (Feb 24, 2011).
Originally assigned to: @beberlei on GitHub.
Jira issue originally created by user dreddy:
My problem : i have a class table inheritance, with only 3 class, one abstract and two concrete.
says AbstractClass, ConcretClassA and ConcreteClassB
All three are declared with @entity
if i already have in my identity map the ConcretClassA with id = 1
when i do an entityManager->find('ConcreteClassB', 1) the identityMap returns me the ConcretClassA with id = 1
that's not correct !
it should return a null value instead
That's because the entityRepository (and all the other doctrine class) call the UnitOfWork tryGetById with the rootEntityName, which in my case is AbstractClass.
See the first line of the entityRepository's find method :
if i change this line to :
the find method return the expected null value.
So, why the UnitOfWork tryGetById method is always called with the rootEntityName ?
@doctrinebot commented on GitHub (Feb 25, 2011):
Comment created by @beberlei:
The identity map HAS to work by root entity name.
Otherwise think of an inheritance hierachy A -> B. Then both $em->find('A', 1); and $em->find('B', 1); should return the same instance.
If this does not hold you are screwed.
Your problem is more subtle, i am not sure what the expected behavior should be here.
@doctrinebot commented on GitHub (Feb 28, 2011):
Comment created by dreddy:
In my sense, the client code should exactly knows the inheritance hierachy, and exactly know what it wants too, so if it asks for an 'A' instance, you can't return a 'B' instance, although A inherit from B.
@doctrinebot commented on GitHub (Feb 28, 2011):
Comment created by @beberlei:
I agree with you except for the last sentence part.
If A->B and you query for B with Id 1 it should treturn an A if it exists. But if B->C and A->C then find(A) should only ever return As or Cs never, Bs.
@doctrinebot commented on GitHub (Feb 28, 2011):
Comment created by dreddy:
I agree.
Perhaps my problem is more specific to the class table inheritance strategy with an abstract class at the top of it.
Here's my identityMap's dump (with my first comment inheritance example) at the time the problem occurs :
'AbstractClass' =>
array
1 => string 'ConcretClassA'
2 => string 'ConcretClassA'
3 => string 'ConcretClassA'
367 => string 'ConcretClassB'
368 => string 'ConcretClassB'
369 => string 'ConcretClassB'
And, at this time, in my code, i would like to know if the id 1 is a ConcretClassB. If the id 1 is a ConcretClassA, i must throw an exception.
So, how could i test if the id 1 is a ConcretClassB or not, if the find('ConcretClassB', 1) method returns me the ConcretClassA 1 ?
thx for your help
@doctrinebot commented on GitHub (Feb 28, 2011):
Comment created by @beberlei:
Cant you just check instanceof?
@doctrinebot commented on GitHub (Feb 28, 2011):
Comment created by dreddy:
Of course i can.
But i thought Doctrine should already have to do this check internally for us. Because we must now be very careful with the find method's returns...
Nevermind, i don't have all the uses case Doctrine have to handle with inheritance in mind; so if you say it is to the client code to check, and it is a completely normal behavior, then it's okay for me.
@doctrinebot commented on GitHub (Feb 28, 2011):
Comment created by @beberlei:
i will change this behavior in the future, i am just making suggestions for you to fix it now. Using instanceof will also be forwards compatible since "null instanceof Class" is always false.
In any case the level of having to be careful is the same, using null instead of an object can get you into troubles aswell.
@doctrinebot commented on GitHub (Mar 3, 2011):
Comment created by dreddy:
Perhaps throw a \Doctrine\ORM\EntityNotFoundException would be a better option than return a null value ?
@doctrinebot commented on GitHub (Mar 4, 2011):
Comment created by @beberlei:
Fixed.
@doctrinebot commented on GitHub (Mar 4, 2011):
Issue was closed with resolution "Fixed"