Open enekochan opened 7 years ago
So, your Collection
contains Proxies, but you're passing a real entity into the contains()
method?
The bug here is possibly that the private
properties are not lazily loaded. Can you try changing the properties to public
, re-generating the proxies and re-running this?
Also, I think this is fixed in master
(3.x
)
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....
I've just hit the very same issue on symfony 3.4.23 and doctrine/orm 2.6.3
Please try to recreate this behaviour on Doctrine ORM only without Symfony in a test.
Hi same here with SF v4.2.4 + doctrine/orm 2.6.3
EDIT Found what broke it down !!!
services:
...
App\EventSubscriber\EntitiesLifecycleSubscriber:
tags: [doctrine.event_subscriber]
<?php
// src/EventSubscriber/EntitiesLifecycleSubscriber.php
namespace App\EventSubscriber;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use App\Entity\Game;
use Doctrine\ORM\Events;
use App\Service\XXX;
class EntitiesLifecycleSubscriber implements EventSubscriber
{
protected $XXX;
public function __construct(XXX $XXX)
{
$this->XXX = $XXX;
}
public function getSubscribedEvents()
{
return [
Events::prePersist,
Events::preUpdate,
];
}
public function preUpdate(LifecycleEventArgs $args)
{
$entity = $args->getObject();
if ($entity instanceof Game) {
$this->setGameIssue($entity);
}
}
public function prePersist(LifecycleEventArgs $args)
{
$entity = $args->getObject();
if ($entity instanceof Game) {
$this->setGameIssue($entity);
}
}
protected function setGameIssue(Game $game)
{
if ($game->getPublished() && is_null($game->getIssue())) {
$game->setIssue($this->XXX->getNextIssue());
}
}
}
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
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.
@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...
It would be extremely helpful if someone could provide a PR with a failing functional that reproduces this behaviour. Any volunteer?
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:
class DoctrineSubscriber implements EventSubscriber
{
/** @var CategoryService */
protected $categoryService;
/** @required */
public function setCategoryService(CategoryService $categoryService)
{
$this->categoryService = $categoryService;
}
public function getSubscribedEvents()
{
return [
// Irrelevant
];
}
}
class CategoryService
{
/** @var EntityManagerInterface */
protected $doctrine;
/** @required */
public function setDoctrineService(EntityManagerInterface $doctrine)
{
$this->doctrine = $doctrine;
}
public function foo()
{
$categories = $this->doctrine->getRepository(Category::class)->findAll();
foreach ($categories as $category) {
dump($this->doctrine->contains($category));
}
}
}
Calling $categoryService->foo()
will dump false
in this case, but if I remove the EventSubscriber (or even remove the dependency on the service) it will show true
instead.
Please try to recreate this behaviour on Doctrine ORM only without Symfony in a test.
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.
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 agetEnabledResources
method that usesCriteria
to get all Resource objects that have astatus
attribute to a certain value (enabled). I just wrote a repository methodfindEnabledResources
for Service that achieves the same result as thegetEnabledResources
method withCriteria
. My intention is to remove the use ofCriteria
in the app.There's a point I store all the enabled resources for a service calling the repository method
findEnabledResources
and then check withcontains
if 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
contains
still returns false, thus entering in the if block.If I use
getEnabledResources
(withCriteria
) thecontains
method 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
. InfindEnabledResources
I'm returning an ArrayCollection of actualAppBundle\Entity\Resource
objects. 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: