To second Christophe's response, most Doctrine entities/documents are plain
PHP objects, so there is no easy way to get the DIC available without
setting it yourself (probably a hacky solution).

To explain how ContainerAware works with controllers, you can trace the
point in Symfony2's core code when controllers are resolved from routing
rules.  Upon their construction, if the controller class implements
ContainerAware, Symfony2 injects the container via a setter method of that
interface.  The base Controller class implements the interface for you, so
this behavior might appear magical for a casual developer.

If you need the DIC for some logic within your model, you may want to
investigate using Doctrine event listeners, which can be services within
your DIC and injected with any other dependency in the DIC you might need.
These differ from lifecycle callbacks (methods within the model class itself
triggered on things like prePersist), in that Doctrine event listeners are
triggered for any model.  So if you need to do something specific for a
given model, you'll likely want your handling method to check the class of
the entity before doing anything.  A good example of how these work would be
to search out some Doctrine 2.0 behaviors on Github, such as Timestampable
or Sluggable - these should have Symfony2 bundles available and are
implemented as Doctrine event listeners in the DIC.

On Sun, Jan 30, 2011 at 3:07 PM, Christophe COEVOET <[email protected]> wrote:

> Le 30/01/2011 20:52, Carlos Sánchez a écrit :
>
>  Hi all,
>> Need to gain access to the services that DIC provides,
>> (entity_manager, event_manager, etc..) from my classes (entity class).
>> How do I do it?
>> Controller class extends ContainerAware, will that be enough?,
>> extending ContainerAware from classes i need to gain access to DIC? Is
>> that to proper way to do it?
>>
>> Thanks in advanced.
>>
>>  Your class has to be define as a service in the DIC to be able to inject
> the container.
> This is not possible for the entities. If you need to access the
> entity_manager, this may mean you need to refactor your code and use a
> custom repository to put this code.
>
> --
> Christophe | Stof
>
>
> --
> If you want to report a vulnerability issue on symfony, please send it to
> security at symfony-project.com
>
> You received this message because you are subscribed to the Google
> Groups "symfony developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]<symfony-devs%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/symfony-devs?hl=en
>



-- 
jeremy mikola

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en

Reply via email to