How to create a custom EntityManager in Symfony2 / Doctrine?

The new guy at Symfony / Doctrine. So kindly instruct me.

Demand. To create a custom EntityManager that will override some methods such as remove (instead of deleting, I want to update and change a parameter like isValid in my class so that the records are never deleted) and find (find the record with a non-zero value isValid) and t .d. and use it instead of the Doctrine EntityManager.

I started reading this topic: Is there a way to specify the Doctrine2 Entitymanager implementation class in Symfony2? and found the answer by user2563451 is not so simple. I got lost when he talked about not following certain approaches (again, there is no place for files that need to be changed).

I looked at EntityManager.php and it specifically says that you should not use the EntityManager class extension. Rather, he asks for an extension to EntityManagerDecorator. When looking at the EntityManagerDecorator, there are no methods available inside it (e.g. create, persist, etc., which I found in EntityManager). Does this mean that I need to create new methods for each individual Entity Manager function?

Since there is no clear definite way to do this, I am confused to begin this work. Also, the Doctrine cookbook is of little use to me, since there is no information available to achieve it.

Therefore, any help regarding the EntityManagerDecorator or EntityManager extension is appreciated.

Best of all, if you can provide me step by step to achieve the same.

Thanks!

Edit 1: my requirement is to use my custom EntityManager instead of Doctrine EntityManager (EM) and modify the delete and find methods to suit my requirements. I'm not sure if I need to reuse the functionality provided by Doctrine EM, or write from scratch.

+6
source share
2 answers

I think you can confuse the manager with the repository.

An EntityManager is nothing more than a service that you use to manage this particular or set of objects.

The repository extends \Doctrine\ORM\EntityRepository , and this is what Doctrine tells how to store your entity in the database.

You can use a combination of these two to achieve what you want.

For instance. Let's take our essence foo

 class Foo { //...other properties protected $isValid; //...Getters and setters } 

Then we have a manager for Foo.

 class FooManager { protected $class; protected $orm; protected $repo; public function __construct(ObjectManager $orm , $class) { $this->orm = $orm; $this->repo = $orm->getRepository($class); $metaData = $orm->getClassMetadata($class); $this->class = $metaData->getName(); } public function create() { $class = $this->getClass(); $foo = new $class; return $foo; } public function findBy(array $criteria) { return $this->repo->findOneBy($criteria); } public function refreshFoo(Foo $foo) { $this->orm->refresh($foo); } public function updateFoo(Foo $foo, $flush = true) { $this->orm->persist($foo); if($flush) { $this->orm->flush(); } } public function getClass() { return $this->class; } } 

We have some basic functions for creating and updating our facility. And now, if you want to “delete” it without deleting it, you can add the following function in the manager.

 public function remove(Foo $foo) { $foo->setIsValid(false); return $this->update($foo); } 

Thus, we update the isValid fields to false and store it in the database. And you will use it like any service inside your controller.

 class MyController extends Controller { public function someAction() { $fooManager = $this->get('my_foo_manager'); $newFoo = $fooManager->create(); //... $fooManager->remove($newFoo); } } 

So now we have the deletion part.

Next, we want to find objects for which isValid set to TRUE.

Honestly, the way I would deal with this is to not even modify find and instead in your controller

 if(!$foo->getIsValid()) { //Throw some kind of error. Or redirect to an error page. } 

But if you want to do it differently. You can just do a repo.

 use Doctrine\ORM\EntityRepository; class FooRepository extends EntityRepository { public function find($id, $lockMode = LockMode::NONE, $lockVersion = null) { //Some custom doctrine query. } } 

We override our own find () function EntityRepository with our own.

Finally, we get everything registered in the right place. For the manager you need to make a service.

 services: my_foo_manager: class: AppBundle\Manager\FooManager arguments: [ "@doctrine.orm.entity_manager" , 'AppBundle\Entity\Foo'] 

And for the repository, you must specify repositoryClass in the ORM definition of your object.

 AppBundle\Entity\Foo: type: entity repositoryClass: AppBundle\Entity\FooRepository table: foos id: id: type: integer generator: {strategy: AUTO} options: {unsigned: true} fields: isValid: type: boolean 

Knowing all this, now you can do quite interesting things with Entities. Hope this helps. Good luck

+3
source

For your use cases, you should use Doctrine Lifecycle Callbacks instead to remove the case and simply override the find method in your object repository.

0
source

All Articles