1

Scenario

Talk about Symfony2. I have an entity with his setters and getters. One of this setter is a bit particular, because have to retrieve some related object, do something and write back ralationship.
What came in my mind, for retrieve those objects, is to use the entity manager and repository related to my entity.

Problem

I haven't the possibility to access Entity Manager directly from entity. One possible solution is to inject the E.M. into this entity but, as far I know, dependency injection is not recommended for entity.

Possible solution

Write a service, inject into it E.M., use service into entity.
Please pay attention to this: I can't instantiate my entity elsewhere because a Bundle (SonataAdmin) is responsible for doin' that.

Is this a good way to do it, or exists a better method?

4

1 回答 1

1

Let's say you're working with the Entity 1. You need to get entity 2.

I would create a service, with a function which get Entity 1 as a parameter. Give E.M. to this service, and get Entity 2.

Entity file isn't made to such a thing, services are here to help you.

于 2012-09-11T14:12:37.707 回答