<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#service.yml
app.resource_upload_listener:
   class: AppBundle\EventListener\ResourceUploadListener
   arguments: ['@app.resource.service']
   tags:
       - {name: doctrine.event_listener, event: prePersist}
       - {name: doctrine.event_listener, event: preUpdate}"><pre class="notranslate"><span class="pl-c"><span class="pl-c">#</span>service.yml</span>
<span class="pl-ent">app.resource_upload_listener</span>:
   <span class="pl-ent">class</span>: <span class="pl-s">AppBundle\EventListener\ResourceUploadListener</span>
   <span class="pl-ent">arguments</span>: <span class="pl-s">['@app.resource.service']</span>
   <span class="pl-ent">tags</span>:
       - <span class="pl-s">{name: doctrine.event_listener, event: prePersist}</span>
       - <span class="pl-s">{name: doctrine.event_listener, event: preUpdate}</span></pre></div>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#listener
    private $uploader;

    public function __construct(ResourceService $uploader)
    {
        $this-&gt;uploader = $uploader;
    }

    public function prePersist(LifecycleEventArgs $args){

        $entity = $args-&gt;getEntity();
        $this-&gt;uploadFile($entity);
    }

    public function preUpdate(PreUpdateEventArgs $args)
    {
        $entity = $args-&gt;getEntity();

        $this-&gt;uploadFile($entity);
    }

    private function uploadFile($entity)
    {
        $file = $entity-&gt;getBrochure();

        if(!$file instanceof UploadedFile){

            return;
        }

        $fileName = $this-&gt;uploader-&gt;upload($file);

        $entity-&gt;setBrochure($fileName);
    }"><pre class="notranslate"><span class="pl-c">#listener</span>
    <span class="pl-k">private</span> <span class="pl-s1"><span class="pl-c1">$</span>uploader</span>;

    <span class="pl-k">public</span> <span class="pl-k">function</span> <span class="pl-en">__construct</span>(<span class="pl-smi"><span class="pl-smi">ResourceService</span></span> <span class="pl-s1"><span class="pl-c1">$</span>uploader</span>)
    {
        <span class="pl-s1"><span class="pl-c1">$</span><span class="pl-smi">this</span></span>-&gt;<span class="pl-c1">uploader</span> = <span class="pl-s1"><span class="pl-c1">$</span>uploader</span>;
    }

    <span class="pl-k">public</span> <span class="pl-k">function</span> <span class="pl-en">prePersist</span>(<span class="pl-smi"><span class="pl-smi">LifecycleEventArgs</span></span> <span class="pl-s1"><span class="pl-c1">$</span>args</span>){

        <span class="pl-s1"><span class="pl-c1">$</span>entity</span> = <span class="pl-s1"><span class="pl-c1">$</span>args</span>-&gt;<span class="pl-en">getEntity</span>();
        <span class="pl-s1"><span class="pl-c1">$</span><span class="pl-smi">this</span></span>-&gt;<span class="pl-en">uploadFile</span>(<span class="pl-s1"><span class="pl-c1">$</span>entity</span>);
    }

    <span class="pl-k">public</span> <span class="pl-k">function</span> <span class="pl-en">preUpdate</span>(<span class="pl-smi"><span class="pl-smi">PreUpdateEventArgs</span></span> <span class="pl-s1"><span class="pl-c1">$</span>args</span>)
    {
        <span class="pl-s1"><span class="pl-c1">$</span>entity</span> = <span class="pl-s1"><span class="pl-c1">$</span>args</span>-&gt;<span class="pl-en">getEntity</span>();

        <span class="pl-s1"><span class="pl-c1">$</span><span class="pl-smi">this</span></span>-&gt;<span class="pl-en">uploadFile</span>(<span class="pl-s1"><span class="pl-c1">$</span>entity</span>);
    }

    <span class="pl-k">private</span> <span class="pl-k">function</span> <span class="pl-en">uploadFile</span>(<span class="pl-s1"><span class="pl-c1">$</span>entity</span>)
    {
        <span class="pl-s1"><span class="pl-c1">$</span>file</span> = <span class="pl-s1"><span class="pl-c1">$</span>entity</span>-&gt;<span class="pl-en">getBrochure</span>();

        <span class="pl-k">if</span>(!<span class="pl-s1"><span class="pl-c1">$</span>file</span> instanceof <span class="pl-v">UploadedFile</span>){

            <span class="pl-k">return</span>;
        }

        <span class="pl-s1"><span class="pl-c1">$</span>fileName</span> = <span class="pl-s1"><span class="pl-c1">$</span><span class="pl-smi">this</span></span>-&gt;<span class="pl-c1">uploader</span>-&gt;<span class="pl-en">upload</span>(<span class="pl-s1"><span class="pl-c1">$</span>file</span>);

        <span class="pl-s1"><span class="pl-c1">$</span>entity</span>-&gt;<span class="pl-en">setBrochure</span>(<span class="pl-s1"><span class="pl-c1">$</span>fileName</span>);
    }</pre></div>
<p dir="auto">I use the event listener to cooperate the upload file, But when start the web server , it thrown an error:  Circular reference detected for service "doctrine.dbal.default_connection", path: "doctrine.dbal.default_connection".</p>
<p dir="auto">Help</p>
<p dir="auto">Hello,<br>
DoctrineBundle have made a change ir orm.xml (added lazy=true):</p>
<div class="highlight highlight-text-xml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content=" &lt;service id=&quot;doctrine.orm.entity_manager.abstract&quot; class=&quot;%doctrine.orm.entity_manager.class%&quot; abstract=&quot;true&quot; lazy=&quot;true&quot; /&gt;"><pre class="notranslate"> &lt;<span class="pl-ent">service</span> <span class="pl-e">id</span>=<span class="pl-s"><span class="pl-pds">"</span>doctrine.orm.entity_manager.abstract<span class="pl-pds">"</span></span> <span class="pl-e">class</span>=<span class="pl-s"><span class="pl-pds">"</span>%doctrine.orm.entity_manager.class%<span class="pl-pds">"</span></span> <span class="pl-e">abstract</span>=<span class="pl-s"><span class="pl-pds">"</span>true<span class="pl-pds">"</span></span> <span class="pl-e">lazy</span>=<span class="pl-s"><span class="pl-pds">"</span>true<span class="pl-pds">"</span></span> /&gt;</pre></div>
<p dir="auto">Now all my lazy services which are actually doctrine listeners stopped working (throws circular error).</p>
<p dir="auto">After couple of hours of debugging I've found out a bug (hopefully) in <code class="notranslate">PhpDumper.php</code> <code class="notranslate">hasReference</code> method.</p>
<p dir="auto">I've added a debugging echo lines in that method and saw these results:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="hasReference: (doctrine.dbal.default_connection) , visited: doctrine.orm.default_listeners.attach_entity_listeners -&gt; listener.event_logger -&gt; doctrine.orm.default_entity_manager -&gt; doctrine.orm.default_entity_manager.delegate (eq: false)
Service: doctrine.orm.default_entity_manager.delegate (lazy), has arguments: [ doctrine.dbal.default_connection, def: Doctrine\ORM\Configuration  ]
hasReference: (doctrine.dbal.default_connection) , visited: doctrine.orm.default_listeners.attach_entity_listeners -&gt; listener.event_logger -&gt; doctrine.orm.default_entity_manager -&gt; doctrine.orm.default_entity_manager.delegate -&gt; doctrine.dbal.default_connection (eq: true)"><pre class="notranslate"><code class="notranslate">hasReference: (doctrine.dbal.default_connection) , visited: doctrine.orm.default_listeners.attach_entity_listeners -&gt; listener.event_logger -&gt; doctrine.orm.default_entity_manager -&gt; doctrine.orm.default_entity_manager.delegate (eq: false)
Service: doctrine.orm.default_entity_manager.delegate (lazy), has arguments: [ doctrine.dbal.default_connection, def: Doctrine\ORM\Configuration  ]
hasReference: (doctrine.dbal.default_connection) , visited: doctrine.orm.default_listeners.attach_entity_listeners -&gt; listener.event_logger -&gt; doctrine.orm.default_entity_manager -&gt; doctrine.orm.default_entity_manager.delegate -&gt; doctrine.dbal.default_connection (eq: true)
</code></pre></div>
<p dir="auto">It looks like hasReference also recursively looking up a lazy services arguments for reference search,<br>
I've thought, is that really logically? Because lazy services is lazy and are instantiated indirectly so more logically would be skip reference search in lazy service definitions, isn't it?</p>
<p dir="auto">more information:<br>
<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="167251361" data-permission-text="Title is private" data-url="https://github.com/doctrine/DoctrineBundle/issues/559" data-hovercard-type="pull_request" data-hovercard-url="/doctrine/DoctrineBundle/pull/559/hovercard?comment_id=241442703&amp;comment_type=issue_comment" href="https://github.com/doctrine/DoctrineBundle/pull/559#issuecomment-241442703">doctrine/DoctrineBundle#559 (comment)</a></p>