. */ namespace Doctrine\ORM; use Doctrine\Common\Collections\AbstractLazyCollection; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Collections\Selectable; use Doctrine\ORM\Persisters\BasicEntityPersister; use Doctrine\ORM\Persisters\EntityPersister; /** * A lazy collection that allow a fast count when using criteria object * * @since 2.5 * @author Michaƫl Gallego */ class LazyCriteriaCollection extends AbstractLazyCollection implements Selectable { /** * @var BasicEntityPersister */ protected $entityPersister; /** * @var Criteria */ protected $criteria; /** * @param EntityPersister $entityPersister * @param Criteria $criteria */ public function __construct(EntityPersister $entityPersister, Criteria $criteria) { $this->entityPersister = $entityPersister; $this->criteria = $criteria; } /** * Do an efficient count on the collection * * @return int */ public function count() { if ($this->isInitialized()) { return $this->collection->count(); } return $this->entityPersister->count($this->criteria); } /** * {@inheritDoc} */ public function matching(Criteria $criteria) { $this->initialize(); return $this->collection->matching($criteria); } /** * {@inheritDoc} */ protected function doInitialize() { $elements = $this->entityPersister->loadCriteria($this->criteria); $this->collection = new ArrayCollection($elements); } }