On 26 January 2014 19:06, Absalom <[email protected]> wrote:

> Hi everyone, I've been searching for an answer and experimenting for a
> whole
> day now. Enough is enough! =)
>
> Simply put, I have two modules; The main Application module which holds a
> User entity and a second one, Auth, for authentication and registration
> that
> tries to make use of this entity. Since I obviously don't want the two
> modules to be dependent on each other I'd like the Auth module to try to
> use
> its own (non-existing) entity "Auth\Entity\User" but this should be
> overridden by the application to "Application\Entity\User".
>

You can use a config flag for that. ZfcUser allows defining what the entity
class would be, for example.

'my_super_cool_module_user_entity_class' => 'MyStuff\User',



> The first case where I try to use the Application modules User entity from
> within the Auth module is when registering and a new User should be
> created.
> Here I have to create a new instance of User and to be able to override it
> I
> use the ServiceLocator, so that I can register an invokable on application
> level, pointing "Auth\Entity\User" to "Application\Entity\User". This
> works,
> but is this really the way to do it?
>

No, the service manager is only supposed to instantiate SERVICES, never
ever ever ever use it to instantiate anything that isn't a service.
Newables should live outside the container. I usually refer to
http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/ when discussing
that.

You can ask the implementor to provide a service that has a "createUser"
method in its public interface. That hides any instantiation logic and
allows the end user to retain control over how a user should be created,
removing a lot of problems that you may find when dealing with constructor
params or required injections (although "required" setter injection is a
stupid idea, some people do that).


> The second case is when I try to login a user. For simplicity sake there's
> no AuthenticationService involved now, I simply fetch the User from it's
> repository using the email. This is where I'm totally stuck. I'm tring to
> use $entityManager->getRepository('Auth\Entity\User'),


Get the repository injected. Define it as a service in your module and
allow the any module implementor to override that service.


> But it seems
> impossible to override this on application level to
> "Application\Entity\User". I've tried all sorts of configurations in
> global.config.php and even directly in the Auth modules config-file to tell
> it to use the Appllication Entities but nothing works.
>

You can just rely on an interface and then use a Doctrine2 listener for the
`loadClassMetadata` event of the ORM to replace any reference to that
interface to the real entity name.


Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

Reply via email to