php - Get entity by ID from multiple entities in Symfony2 -


suppose have set of entities:

$entities = $em->getrepository('mybundle:entity')->findby(array('cat' => 1)); 

what best way pick single entity out of set id? of course can search loop or array_filter, e.g.:

$entity = null; foreach ($entities $_entity) {     if ($_entity->getid() == $id) {         $entity = $_entity;         break;     } } 

but maybe there build in symfony/doctrine method that?

hi can use filter method arraycollection class, it's not different looping entities you're doing

$idtosearch = $n;  $newcollection = $entities->filter(     function($entity) use ($idtosearch) {         return $entity->getid() == $idtosearch;     } );  

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -