Skip to content

Latest commit

 

History

History
110 lines (88 loc) · 3.23 KB

using-doctrine-extra.md

File metadata and controls

110 lines (88 loc) · 3.23 KB

Refugis - DoctrineExtra - Using Doctrine Extra

This library introduce some Doctrine features and helpers.

objects is often a shortcut reference to entities or documents.

EntityIterator and DocumentIterator

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);
}

EntityRepository and DocumentRepository

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.

How to use

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

Other features

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!