php - Circular Reference on preFlush -


i'm having trouble dependency injection. i'm trying save record db in preflush listener. i'm saving record db via own service ( custom logging service ). i've tried few ways none working, i've tried every google / stackover flow result i've found no luck yet i'm afraid.

here's setup logger class, private variables have been ommitted set ( use request stack , translator else in class, wasn't sure if should ommit question ):

config:

core.logger:     class: xxx\corebundle\logger     arguments: [@request_stack, @doctrine.orm.entity_manager, @translator] 

class:

public function __construct(requeststack $requeststack, entitymanager $em, translatorinterface $t)     {         $this->requeststack = $requeststack;         $this->em = $em         $this->t = $t;     } public function addlogentitychange($uow, $entity) {         $changeset = $uow->getentitychangeset($entity);         foreach($changeset $key => $value) {             $logentity = new log();             //setstuff..             $this->em->persist($logentity);         }         $this->em->flush();     } 

and here's interface code

config:

xxxcore.loggerlistener:   class: xxx\corebundle\listener\loggerlistener   calls:     - [ setlogger, [@core.logger] ]   tags:     - { name: doctrine.event_listener, event: preflush } 

interface:

    public function setlogger($logger)     {         $this->l = $logger;     }     public function preflush(preflusheventargs $args)     {         $em = $args->getentitymanager();         $uow = $em->getunitofwork();         $uow->computechangesets();          foreach ($uow->getscheduledentityupdates() $entity) {             if($entity instanceof loggerinterface) {                 $this->l->addlogentitychange($uow, $entity);             }          }     } 

this leads the

circular reference detected service "doctrine.dbal.xxx_connection", path: "doctrine.dbal.xxx_connection". 

i've tried setting entity manager in logger class manually, removing dependancy injection , using setentitymanager function timesout page. i've tried inserting service_container didnt either.

when calling functions manually after persist entity works, automate via interface preflush listener.

any / insight appreciated!

interesting. has come before: symfony circular reference exception doctrine onflush event listener service

somewhere in doctrine tagging process there must compiler pass injecting listeners entity manager , causing circular reference.

i checked doctrinebundle code , yep, entity manager dependent on it's listeners , of course if listener dependent on entity manager circular reference.

i duplicated problem with:

## services.yml cerad_project_level_logger:   class: cerad\projectlevel\levellogger   arguments:     # - '@doctrine.orm.entity_manager'  cerad_project_level_listener:   class: cerad\projectlevel\levellistener   arguments:     - '@cerad_project_level_logger'   tags:     - { name: doctrine.event_listener, event: preflush } 

the work around pass entity manager in logger method instead of injecting it.

    foreach ($uow->getscheduledentityupdates() $entity) {         if($entity instanceof loggerinterface) {             $this->l->addlogentitychange($em, $uow, $entity);         } 

i'm confident code still not work intended since preflush called after em->flush , end in loop @ least past circular reference error.

all these problems go away if make logger entity manager. besides, want log production database?


Comments

Popular posts from this blog

How to show in django cms breadcrumbs full path? -

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -