-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Circular reference detected for service "doctrine.orm.default_entity_manager" #29693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You shoud not inject <?php
namespace App\Listeners;
use App\Entity\Post;
use App\Entity\Attachment;
use Doctrine\ORM\Event\PreUpdateEventArgs;
class PostListener
{
public function preUpdate(Post $post, PreUpdateEventArgs $args)
{
$attachmentRepository = $args->getEntityManager()->getRepository(Attachment::class);
dump($attachmentRepository->findAll());
}
} |
Hey there mate, thank you for the response, Yes using the EntityManager that's in $args would work ,but the problem is i have the same exact code where i inject the AttachmentRepository in PostListener's construcor and it worksm which makes me think this is a bug with the new version because i can find any chnages log reporting any changes in terms of injecting Repositories in Listeners. |
@konshensx16 thanks for reporting this issue. You said that this works in |
Maybe caused by the same change that causes #29637? Can you check if the commit mentioned there made the difference in your application too? |
Hello @javiereguiluz and @xabbuh thanks for replying to this, i just want to address one weird thing i just noticed like 5 minutes ago when i was doing more tests, the code i posted above isn't working obviously and throwing the exception, but this one WORKS and i cannot seem to undertand why: <?php
namespace App\Listeners;
use App\Entity\Attachment;
use App\Entity\Post;
use App\Repository\AttachmentRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Symfony\Component\Security\Core\Security;
class PostListener
{
// i need to get the current logged in user
/**
* @var Security
*/
private $security;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var AttachmentRepository
*/
private $attachmentRepository;
public function __construct(Security $security,
EntityManagerInterface $entityManager,
AttachmentRepository $attachmentRepository
)
{
$this->security = $security;
$this->entityManager = $entityManager;
$this->attachmentRepository = $attachmentRepository;
}
public function preUpdate(Post $post, PreUpdateEventArgs $args)
{
dump($this->attachmentRepository->findAll()); die;
}
} You guys have any idea why this would work and the other one (the code in the issue) won't? Does injecting the EntityManagerInterface has something to do with it ? EditRemoving the EntityManagerInterface will result in the exception being throw, and this is for the 4.1.6 which i intially said works fine, but when removing the EntityManagerInterface it throws the exception as well, i'm sorry as i was not aware of this. |
Did to try lazy services https://symfony.com/doc/current/service_container/lazy_services.html ? This should do the trick but if not, try injecting repository with |
…kas) This PR was merged into the 3.4 branch. Discussion ---------- [DI] Fix dumping Doctrine-like service graphs | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #30017 #29637 #29693 | License | MIT | Doc PR | - I'm unable to provide a reproducer for this, the required service reference graph is too crazy, but that does the job :) Commits ------- ed96830 [DI] Fix dumping Doctrine-like service graphs
Symfony version(s) affected: 4.2.1
Description
ServiceCircularReferenceException is thrown when i try to inject a repository in a listener's constructor, one important note is that i have the same exact code that i'm posting right here in a 4.1.6 project and it works just fine, here are the important parts of the code:
PostListener (where seems to be the problem):
services.yaml:
Post.php:
Attachment.php:
How to reproduce
Git repository to recreate the problem: https://github.com/konshensx16/circular-reference
Navigating to localhost:8000/post should be enough since i can't even start the server because of the problem, but if that doesn't work, triggering the PostListener by updating a post's content field should 100% throw the exception.
Additional context
As mentioned before i have the same exact code in version 4.1.6 and it works perfectly fine.
Thank you in advance :)
The text was updated successfully, but these errors were encountered: