use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Zenstruck\Porpaginas\Doctrine\ORMQueryIterateResult;
use Zenstruck\Porpaginas\Result;
trait ORMRepository
{
/** @var EntityManager */
protected $em;
/**
* This annotation triggers setter injection when autowiring.
*
* @required
*/
public function setEntityManager(EntityManager $em): void
{
$this->em = $em;
}
public function save($object): void
{
$this->em->persist($object);
$this->em->flush();
}
public function count(): int
{
return $this->getIterator()->count();
}
public function getIterator(): Result
{
return new ORMQueryIterateResult($this->em, $this->qb());
}
protected function repo(): EntityRepository
{
return $this->em->getRepository($this->getEntityName());
}
protected function qb(string $alias = 'entity'): QueryBuilder
{
return $this->repo()->createQueryBuilder($alias);
}
abstract protected function getEntityName(): string;
}
Last active
July 6, 2017 15:09
-
-
Save kbond/2dfdf25f9b2237ec4cdd742462d2fa73 to your computer and use it in GitHub Desktop.
ORMRepository trait
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment