Injecting SecurityContext into a Listener prePersist or preUpdate in Symfony2 to get User in a createdBy or updatedBy Causes Circular Reference Error

前端 未结 4 2048
遥遥无期
遥遥无期 2020-11-29 01:38

I setup a listener class where i\'ll set the ownerid column on any doctrine prePersist. My services.yml file looks like this ...

services:
my.listener:
             


        
4条回答
  •  离开以前
    2020-11-29 02:15

    I use the doctrine config files to set preUpdate or prePersist methods:

    Project\MainBundle\Entity\YourEntity:
        type: entity
        table: yourentities
        repositoryClass: Project\MainBundle\Repository\YourEntitytRepository
        fields:
            id:
                type: integer
                id: true
                generator:
                    strategy: AUTO
    
        lifecycleCallbacks:
            prePersist: [methodNameHere]
            preUpdate: [anotherMethodHere]
    

    And the methods are declared in the entity, this way you don't need a listener and if you need a more general method you can make a BaseEntity to keep that method and extend the other entites from that. Hope it helps!

提交回复
热议问题