<?php
use PHPSnippets\Db\Entity\Test;
use Doctrine\ORM\Query;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Selectable;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\ArrayCollection;
// function for collection output
$print = function(Collection $result) {
$output = [];
foreach ($result as $entity) {
$output[] = $entity->name;
}
echo implode(', ', $output) .
str_repeat(PHP_EOL, 2);
};
$repo = $entityManager->getRepository(Test::CLASS);
// similar to findAll() but returns result as Collection
$collection = $repo->matching(Criteria::create());
$print($collection);
if ($collection instanceOf Selectable) {
// expression to filter collection
$expr = Criteria::expr()->eq('name', 'foo');
$criteria = Criteria::create();
$criteria->where($expr);
// This works directly with the collection
$collection2 = $collection->matching($criteria);
$print($collection2);
}