Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Last active March 22, 2016 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdevilopers/c94295b790b3ea5d3043 to your computer and use it in GitHub Desktop.
Save webdevilopers/c94295b790b3ea5d3043 to your computer and use it in GitHub Desktop.
Circular reference detected for service "doctrine.dbal.default_connection", path: "doctrine.dbal.default_connection".
<?php
namespace Acme\AppBundle\Service;
use Doctrine\ORM\EntityManager;
use GuzzleHttp\Exception\RequestException;
/**
* @todo Inject mailer
*/
abstract class Api
{
public $client;
public $mailer;
public $entityManager;
public function getClient() {
return $this->client;
}
public function setClient($client) {
$this->client = $client;
}
public function getMailer() {
return $this->mailer;
}
public function setMailer(\Swift_Mailer $mailer) {
$this->mailer = $mailer;
}
function getEntityManager() {
return $this->entityManager;
}
function setEntityManager(EntityManager $entityManager) {
$this->entityManager = $entityManager;
}
}
<?php
namespace Acme\AppBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
// for Doctrine 2.4: Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Acme\AppBundle\Service\Api\Configuration as ApiService;
use Acme\AppBundle\Entity\Configuration;
class ConfigurationListener
{
private $apiService;
public function __construct(ApiService $apiService)
{
$this->apiService = $apiService;
}
public function getApiService() {
return $this->apiService;
}
public function setApiService($apiService) {
$this->apiService = $apiService;
}
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$entityManager = $args->getEntityManager();
if ($entity instanceof Configuration) {
$apiService = $this->getApiService();
$apiService->sendConfiguration();
}
}
}
<?php
namespace Acme\AppBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
// for Doctrine 2.4: Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Acme\AppBundle\Entity\Configuration;
class ConfigurationListener
{
private $apiService;
public function __construct($container)
{
$this->container = $container;
}
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$entityManager = $args->getEntityManager();
if ($entity instanceof Configuration) {
$apiService = $this->container->get('api.configuration');
$apiService->sendConfiguration();
}
}
}
services:
api.listener.configuration:
class: Acme\AppBundle\EventListener\ConfigurationListener
arguments:
api_service: "@api.configuration"
tags:
- { name: doctrine.event_listener, event: postPersist }
writer.configuration_request:
class: Acme\AppBundle\Writer\ConfigurationRequest
api.abstract:
abstract: true
class: Acme\AppBundle\Service\Api
calls:
- [setClient, ["@api.client"]]
- [setMailer, ["@mailer"]]
- [setEntityManager, ["@doctrine.orm.entity_manager"]]
api.configuration:
class: "Acme\AppBundle\Service\Api\Configuration"
parent: api.abstract
calls:
- [setWriter, ["@writer.configuration_request"]]
api.client:
class: GuzzleHttp\Client
services:
api.listener.configuration:
class: Acme\AppBundle\EventListener\ConfigurationListener
tags:
- { name: doctrine.event_listener, event: postPersist }
calls:
- [setApiService, ["@api.configuration"]]
writer.configuration_request:
class: Acme\AppBundle\Writer\ConfigurationRequest
api.abstract:
abstract: true
class: Acme\AppBundle\Service\Api
calls:
- [setClient, ["@api.client"]]
- [setMailer, ["@mailer"]]
- [setEntityManager, ["@doctrine.orm.entity_manager"]]
api.configuration:
class: "Acme\AppBundle\Service\Api\Configuration"
parent: api.abstract
calls:
- [setWriter, ["@writer.configuration_request"]]
api.client:
class: GuzzleHttp\Client
services:
api.listener.configuration:
class: Acme\AppBundle\EventListener\ConfigurationListener
arguments:
container: "@service_container"
tags:
- { name: doctrine.event_listener, event: postPersist }
writer.configuration_request:
class: Acme\AppBundle\Writer\ConfigurationRequest
api.abstract:
abstract: true
class: Acme\AppBundle\Service\Api
calls:
- [setClient, ["@api.client"]]
- [setMailer, ["@mailer"]]
- [setEntityManager, ["@doctrine.orm.entity_manager"]]
api.configuration:
class: "Acme\AppBundle\Service\Api\Configuration"
parent: api.abstract
calls:
- [setWriter, ["@writer.configuration_request"]]
api.client:
class: GuzzleHttp\Client
@webdevilopers
Copy link
Author

Exception:
[Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException]

Circular reference detected for service "doctrine.dbal.default_connection", path: "doctrine.dbal.default_connection".**

Related issues:

Discussion:

@webdevilopers
Copy link
Author

A current workaround is to inject the complete container and call the api.configuration service from there.

Bad practice?
http://stackoverflow.com/questions/7561013/injecting-securitycontext-into-a-listener-prepersist-or-preupdate-in-symfony2-to#answer-7562976

@webdevilopers
Copy link
Author

Since my listener does not depend on the postPersist event I replaced the tags inside the listener from doctrine.event_listener to kernel.event_listener.

@TomasVotruba
Copy link

Any progress on this? Came across the same issue.

Could it be simplified to: doctrine subscriber/listener with dependency?

@murnieza
Copy link

Any progress yet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment