Get service container from entity in symfony 2.1 (Doctrine)

前端 未结 2 1147
面向向阳花
面向向阳花 2020-12-05 08:30

How to use entity as service in doctrine (Using Symfony 2.1).

Example usage:



        
2条回答
  •  一整个雨季
    2020-12-05 09:04

    An entity is a data model and should only hold data (and not have any dependencies on services). If you want to modify your model in case of a certain event (PrePersist in your case) you should look into making a Doctrine listener for that. You can inject the container when defining the listener:

    services:
        my.listener:
            class: Acme\SearchBundle\Listener\YourListener
            arguments: [@your_service_dependency_or_the_container_here]
            tags:
                - { name: doctrine.event_listener, event: prePersist }
    

提交回复
热议问题