How to access repository methods for an entity in symfony2?

前端 未结 4 583
青春惊慌失措
青春惊慌失措 2020-12-15 22:10

I am stuck with a problem please help me with it. Here is the scenarario:

I have an entity \"User\" and corresponding repository \"UserRepository\", inside my entit

4条回答
  •  自闭症患者
    2020-12-15 23:05

    You can use the postLoad event from doctrine and inject everything you want into the entity. The event listener looks like:

        getEntity();
            if(!($document instanceof MyEntity)){
                return;
            }
            $document->setEntityManager($eventArgs->getEntityManager());
    
        }
    }
    

    and service.yml:

    services:
    app.myentity.listener:
      class: AppBundle\EventListener\MyEntityListener
      tags:
        - { name: doctrine.event_listener, event: postLoad }
    

    Of cource your Entity needs the method setEntityManager and your're ready.

提交回复
热议问题