mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
'contains' returns false even the element is contained #5322
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 @enekochan on GitHub (Nov 13, 2016).
Originally assigned to: @enekochan on GitHub.
I have 2 entities, Service and Resource, with a ManyToMany relation between them. Service has the usual
getResources()method and Resource thegetServices()method. The Service class also has agetEnabledResourcesmethod that usesCriteriato get all Resource objects that have astatusattribute to a certain value (enabled). I just wrote a repository methodfindEnabledResourcesfor Service that achieves the same result as thegetEnabledResourcesmethod withCriteria. My intention is to remove the use ofCriteriain the app.There's a point I store all the enabled resources for a service calling the repository method
findEnabledResourcesand then check withcontainsif each one of those resources are contained in a Service object with something like this:I know for sure that a resource object is contained (I can see it while debugging) but
containsstill returns false, thus entering in the if block.If I use
getEnabledResources(withCriteria) thecontainsmethod works just fine!What I've been able to see while debugging is that
$service->getResources()returns a Doctrine\ORM\PersistentCollection with the collection having proxie objects with classProxies\__CG__\AppBundle\Entity\Resource. InfindEnabledResourcesI'm returning an ArrayCollection of actualAppBundle\Entity\Resourceobjects. I've tried using EAGER fetch but still get the same problem.I'm using doctrine/orm and doctrine/dbal on dev-master".
Resumed entity classes and repository:
@mpdude commented on GitHub (Apr 12, 2018):
So, your
Collectioncontains Proxies, but you're passing a real entity into thecontains()method?@Ocramius commented on GitHub (Apr 13, 2018):
The bug here is possibly that the
privateproperties are not lazily loaded. Can you try changing the properties topublic, re-generating the proxies and re-running this?Also, I think this is fixed in
master(3.x)@TheCelavi commented on GitHub (Jun 2, 2018):
I do not know if this helps, gives additional clues, or it is totally unrelated, we just had similar issue, everything worked fine, until Symfony upgrade from 3.4.2 to 3.4.11, and s*** hits the fan.
https://github.com/FriendsOfSymfony/FOSMessageBundle/issues/319#issuecomment-394083941
Downgrade fixed issue. I will probably investigate which symfony 3.4.x version broke a thing, if I get anything useful, will share....
@kyeno commented on GitHub (Mar 22, 2019):
I've just hit the very same issue on symfony 3.4.23 and doctrine/orm 2.6.3
@SenseException commented on GitHub (Mar 24, 2019):
Please try to recreate this behaviour on Doctrine ORM only without Symfony in a test.
@quazardous commented on GitHub (Apr 3, 2019):
Hi same here with SF v4.2.4 + doctrine/orm 2.6.3
EDIT Found what broke it down !!!
but my problem is on User / Group ManyToMany relation so it's some kind of side effect of plugin a
event_subscriber...EDIT2:
using doctrine entity listener is a NO GO too
https://symfony.com/doc/master/bundles/DoctrineBundle/entity-listeners.html
@yellow1912 commented on GitHub (Apr 30, 2019):
I think I'm running into this issue or at least something very similar. I'm on Doctrine ORM 2.6.3 and running into this very weird case where an entity object is queried via the repository just 1 line above, then if I use the objectManager to check for "contains" it returns false.
Edit: actually my issue is something different, I have noticed it just now. Going to delete this message.
@AntoniusGolly commented on GitHub (Jul 7, 2019):
@yellow1912 can you tell me anyways? I just need a hint. I have the exact same issue. I can see the entity in the debugger just fine, but still
contains()returns false. Maybe your solution gives me a hint to investigate...@lcobucci commented on GitHub (Sep 5, 2019):
It would be extremely helpful if someone could provide a PR with a failing functional that reproduces this behaviour. Any volunteer?
@hubertnnn commented on GitHub (Nov 13, 2019):
I am not sure if its the same problem, but I got here trying to figure out similar issue.
What I found is that if you use a service inside doctrine eventSubscriber then the service will receive a broken EntityManager.
Here is some code:
Calling
$categoryService->foo()will dumpfalsein this case, but if I remove the EventSubscriber (or even remove the dependency on the service) it will showtrueinstead.@SenseException commented on GitHub (Nov 17, 2019):
Please try to recreate this behaviour on Doctrine ORM only without Symfony in a test.
@RafaelKr commented on GitHub (Jul 5, 2024):
I had a similar problem and want to leave this here just in case someone stumbles upon this.
In my Symfony project I manually created a Proxy Object by using
$this->entityManager->getProxyFactory()->getProxy($entityClass, $id).But this doesn't attach the object to the EntityManager. After some debugging I found out I need to use
$this->entityManager->getReference($entityClass, $id)instead.