php - Difference between $em->find(..) and $em->getRepository(..)->find(..) -


there entity

/**  * @orm\entity(repositoryclass="some\namspace\customrepository")  * @orm\table(name="image_type")  */ class myentity{...} 

and customrepository extends entityrepository override methods find or findall

documentation says:

// $em instanceof entitymanager $user = $em->find('myproject\domain\user', $id); 

essentially, entitymanager#find() shortcut following:

$user = $em->getrepository('myproject\domain\user')->find($id); 

link:doctrine-orm.readthedocs.org

but customrepository works $em->getrepository('entities\myentity')->find($id)

using $em->find('entities\myentity',$id); ignoring overrided methods in customrepository


  • so bug?
  • or there's difference between construcions?
  • how can overide find,finall,... methods entity without overriding entitymanager?

edit (1)

using composer:

"require": {     "doctrine/orm": "~2.4" }, 

find code:

public function find($entityname, $id, $lockmode = null, $lockversion = null) {     $class = $this->metadatafactory->getmetadatafor(ltrim($entityname, '\\'));      if ( ! is_array($id)) {         if ($class->isidentifiercomposite) {             throw orminvalidargumentexception::invalidcompositeidentifier();         }          $id = array($class->identifier[0] => $id);     }     ........... other ~100 lines } 

note: behavior explained here doctrine orm 2.3+. in 2.2 , before, "reverse" one.

so bug?

nope, it's way use entity manager "faster" without loading repository. behind, repository loads entitymanager execute find method.

or there's difference between construcions?

performances, maybe, maybe need bench on this. basically, it's exactly same

what $em->find(...) does: load single entity primary key.

what $em->getrepository(...)->find(...) does:
runs this: return $this->_em->find($this->_entityname, $id, $lockmode, $lockversion);.
loads $em->find(...) method.
it's same :)

how can overide find,findall, ... methods entity without overriding entitymanager?

create custom repository, either per-entity.

if per-entity, you'll have add "repositoryclass" option in "@entity" annotation, , specify repository class in it, you'll able override every method entity.

see docs know more custom repository use

you can globally. create own entityrepository, make extend doctrine\orm\entityrepository class, , can override different functions.
here example of entityrepository overriding doctrine's default repository, on orbitale/toolsbundle:baseentityrepository.php, if want this, you'll have change "default repository class name" in orm's configuration.

you don't have override entitymanager, it's powerful enough used directly as-is, , many things internally should never broken when doing fuzzy stuff in overriding it.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -