D
D
Dromok2014-02-21 14:37:54
Doctrine ORM
Dromok, 2014-02-21 14:37:54

How to access EntityManager from Entity? (Zend2+Doctrine2)?

Greetings!
I decided to move the business logic to Entity.
But it is not clear how to access the EntityManager from within Entity.
Example in controller:
$em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$data = $form->getData();
$user = new User();
$user->setUsername($data['username']);
$user->setNotifyEmail($data['username']);
$user->setPassword($data['password']);
$em->persist($user);
$em->flush();
This is user creation. I would like to transfer to Entity in the createUser function. But I can't figure out how to access the EntityManager there, since $this->getServiceLocator()->get('Doctrine\ORM\EntityManager'); does not work.
Thanks in advance for your help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2014-02-21
Protko @Fesor

entity should not contain business logic, it is just a mapping of the subject area. Actually, therefore, it should not have access to the entity manager or at least somehow know how it is stored and processed. Business logic should be stored in the service layer.
You should have a userManager service which should have a createUser method.

C
cawakharkov, 2014-02-22
@cawakharkov

It will also be useful to use custom repositories along with the service layer - documentation , post on habré

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question