php - How to get the instance of Kernel in the Entity class in Symfony2 -
The headline explains the question very well. I am in the Lifecycle Callback of Principle Entity Class and I want to do some additional DB entries. For this, I need to get an example of a kernel. how can I do this?
An entity requires a container / kernel, strong> incorrect . An entity should not be aware of any services. Why is it like this?
Basically, a unit is an object that represents an object. An entity is used mostly in relational databases, but you can use this unit for any other time (sort it, install it with an HTTP layer ...). You want to consider your unit as unit trials, this means that you should be able to easily install your unit easily, without much, without much business logic.
You should take your logic in the second layer, which will instanciate your institution in case of your use, I think the easiest way is to use A.
services.yml And you can go there, your organization remains flat for no dependencies, and you can easily check your event listener
Services: acme_foo.bar_listener: Category: Acme \ Fundabal \ Bar \ Barlistner Arguments <->
Use symphonic / component \ HttpKernel \ KernelInterface; Use the theory / ORM \ program \ LifecycleEventArgs; Use Acme \ FooBundle \ Entity \ Bar; Class barlister (protected $ kernel; / ** * constructor * * @ param kernel interface $ kernel a kernel example * / public function __ composition (kernel interface $ kernel) {$ this-> kernel = $ kernel;} / ** * Post * On load * Once the unit is loaded * This method will be trigerred * @ Ultimate LifecycleEventArgs $ args theory program * / Public function postLoad (LifecycleEventArgs $ args) {$ entity = $ args-> GetEntity (); if (! ($ Unit times of example)) {return;} $ institution-> set environment ($ this-> kernel- & gt; GetEnvironment ());}}
Comments
Post a Comment