symfony - Doctrine Criteria not working unless collection is initialized -


i'm using criteria elements first time, think simple enough shouldn't having problem. of course, problem has appeared.

i have onetomany relation defined this:

/**  * @orm\onetomany(targetentity="expedientesmensajes", mappedby="expediente", cascade={"remove"})  * @orm\joincolumn(name="id", referencedcolumnname="id_expediente")  */ private $mensajes; 

each of has relation, this:

/**  * @orm\onetomany(targetentity="expedientesmensajesleidos", mappedby="mensaje")  * @orm\joincolumn(name="id", referencedcolumnname="id_mensaje")  */ private $mensajesleidos; 

now, in entity first relation, have method:

function isleidopor($idcontrolacceso = null) {     // http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-associations.html#filtering-collections     $criteria = criteria::create()         ->where(criteria::expr()->eq('idcontrolacceso', $idcontrolacceso))         ->setmaxresults(1);      // el expediente es leído si hay mensajes...     if (0 < count($this->getmensajes())) {         // y todos han sido leidos por el control de acceso que pasamos.         // (no se puede usar un criteria para campos en los campos de los miembros de la colección.)         foreach ($this->getmensajes() $mensaje) {             $mensaje->getmensajesleidos()[0];             if (0 == count($mensaje->getmensajesleidos()->matching($criteria))) {                 return false;             }         }          return true;     } 

it iterates on elements of first relation, , uses defined criteria on each of them, getting subset of collection of elements second relation.

when first relation $mensajes 1 element works. but, when collection greater that, example 3 elements, matching() method returns empty array when i'm seeing on database there elements match requirements.

i've been able work doing

$mensaje->getmensajesleidos()[0]; 

right before matching() method, afaik, think throws away whole benefit of using criteria elements, because initializes collection. i've seen working when debugging , asking debugger first element, same instruction.

does have idea of happening here?

thanks in advance.

i rewritted code in order not use several times same criteria. maybe problem come reuse of not sure.

$criteria = criteria::create() ->where(criteria::expr()->eq('idcontrolacceso', $idcontrolacceso)) ->setmaxresults(1);  // array number of leidos each mensaje $mappedoccurrencies = $this     ->getmensajes()     ->map(function(expedientesmensajes $mensaje) use ($criteria) {         $mensaje             ->getmensajesleidos()             ->matching($criteria)             ->count();     }); // if have @ least mensaje 0 leidos, return false (true otherwise) return !in_array(0, $mappedoccurrencies); 

Comments