This library introduce some Doctrine features and helpers.
objects
is often a shortcut reference to entities
or documents
.
Given an instance of Doctrine\ORM\QueryBuilder
or Doctrine\ODM\MongoDB\Query\Builder
the iterator allows you to iterate a single entity/document query confortably:
ORM
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Refugis\DoctrineExtra\ORM\EntityIterator;
/** @var EntityManagerInterface $em */
$em = // The EntityManagerInterface instance.
$qb = $em->createQueryBuilder();
$qb
->select('u')
->from(User::class, 'u')
;
$users = new EntityIterator($qb);
foreach ($users as $user) {
var_dump($user instanceof User);
}
MongoDB:
use App\Document\User;
use Doctrine\ODM\MongoDB\DocumentManager;
use Refugis\DoctrineExtra\ODM\MongoDB\DocumentIterator;
/** @var DocumentManager $dm */
$dm = // The EntityManagerInterface instance.
$qb = $dm->createQueryBuilder(User::class);
$users = new DocumentIterator($qb);
foreach ($users as $user) {
var_dump($user instanceof User);
}
These two classes are just an extension of the base Doctrine Repository class with some utility methods:
public function all(): ObjectIterator
returns all objects matching that object.
public function count(array $criteria = []): int
returns an integer representing the count of all objects matching the given criteria.
public function findOneByCached(array $criteria, array $orderBy = null, int $ttl = 28800)
executes the base findOneBy
and caches the results.
public function findByCached(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null, int $ttl = 28800)
executes the base findBy
method and caches the results.
public function get($id, $lockMode = null, $lockVersion = null)
executes the base find
method and throws a NoResultException
if no result has been found.
public function getOneBy(array $criteria, ?array $orderBy = null)
executes the base findOneBy
method and throws a NoResultException
if no result has been found.
public function getOneByCached(array $criteria, ?array $orderBy = null, int $ttl = 28800)
executes the findOneByCached
method and throws a NoResultException
if no result has been found.
Unfortunately, Doctrine\ODM\MongoDB
does not support cache.
Just set the default_repository_class
in the Doctrine configuration:
ORM
doctrine:
orm:
default_repository_class: Refugis\DoctrineExtra\ORM\EntityRepository
MongoDB:
doctrine_mongodb:
document_managers:
default_repository_class: Refugis\DoctrineExtra\ODM\MongoDB\DocumentRepository
This library exposes also some features like Timestampable
, TimeSpan
and Enable
interfaces.
These simple interfaces helps adding to your objects. Just implement those interfaces (or use the traits), register the event listeners and you're done!