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
Post a Comment