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 {
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();
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()) {
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) {
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