php - How exactly do I create a custom EntityManager in Symfony2/Doctrine? -
new guy @ symfony/doctrine. kindly guide me.
requirement: create custom entitymanager override of methods remove (instead of remove, want perform update , modify parameter isvalid in class, records never deleted) , find ( find record has non 0 isvalid ) etc , use instead of doctrine's entitymanager.
i started reading through thread: is there way specify doctrine2 entitymanager implementation class in symfony2? , found answer user2563451 not straightforward. got lost when talks not follow approaches (again no location of files modified).
i have looked @ entitymanager.php , tells not use extend entitymanager class. rather asks extend entitymanagerdecorator. on looking @ entitymanagerdecorator, there no methods available inside (like create, persist, etc found in entitymanager) mean need create new methods each , every single entity manager functionality ?
since there no clear defined way done, confused thing started. doctrine cookbook of little use me not have information achieve this.
so regarding extending of entitymanagerdecorator or entitymanager appreciated.
best if can provide me step step directions achieve same.
thanks !
edit 1: requirement use custom entitymanager instead of doctrine's entitymanager (em) , modify 'remove' , 'find' methods per requirements. not sure whether need reuse functionality provided doctrine's em or write scratch.
i think may confusing manager repository.
an entitymanager nothing more service use manage specific or collection of entities.
a repository extends \doctrine\orm\entityrepository
, tells doctrine how store entity in database.
you can use combination of these 2 achieve want.
for example. let's take our entity foo
class foo { //...other properties protected $isvalid; //...getters , setters }
we have manager 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 basic functions creating , updating our object. , if wanted "remove" without deleting it, can add following function in manager.
public function remove(foo $foo) { $foo->setisvalid(false); return $this->update($foo); }
this way, update isvalid
fields false , persist database. , you'd use service inside controller.
class mycontroller extends controller { public function someaction() { $foomanager = $this->get('my_foo_manager'); $newfoo = $foomanager->create(); //... $foomanager->remove($newfoo); } }
so we've got remove part.
next, want find entities isvalid
set true.
honestly, way i'd handle not modify find , instead in controller
if(!$foo->getisvalid()) { //throw kind of error. or redirect error page. }
but if want other way. can make repo.
use doctrine\orm\entityrepository; class foorepository extends entityrepository { public function find($id, $lockmode = lockmode::none, $lockversion = null) { //some custom doctrine query. } }
we override entityrepository's native find() function our own.
finally of registered in right places. manager you've got make service.
services: my_foo_manager: class: appbundle\manager\foomanager arguments: [ "@doctrine.orm.entity_manager" , 'appbundle\entity\foo']
and repository, must specify repositoryclass
in orm definition of entity.
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 of can pretty cool things entities. hope helped. luck!
Comments
Post a Comment