Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Circular reference detected for service "doctrine.orm.default_entity_manager" #29693

Closed
@konshensx16

Description

@konshensx16

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):

<?php

namespace App\Listeners;

use App\Entity\Post;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use App\Repository\AttachmentRepository;

class PostListener
{
    private $attachmentRepository;

    public function __construct(AttachmentRepository $attachmentRepository)
    {
        $this->$attachmentRepository = $attachmentRepository;
    }

    public function preUpdate(Post $post, PreUpdateEventArgs $args)
    {
        dump($this->attachmentRepository->findAll());
    }
}

services.yaml:

services:
    # default configuration for services in *this* file
    #...... default configuration removed for clarity 
    App\Listeners\PostListener:
        tags:
            - { name: doctrine.orm.entity_listener }

Post.php:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\PostRepository")
 * @ORM\EntityListeners({"App\Listeners\PostListener"})
 */
class Post
{

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Attachment", mappedBy="post")
     *
     * @var [type]
     */
    private $attachments;
}

Attachment.php:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\AttachmentRepository")
 */
class Attachment
{
    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Post", inversedBy="attachments")
     *
     * @var [type]
     */
    private $post;
 
}

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 :)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions