|
1 | 1 | Fazland - ApiPlatformBundle - Doctrine Features |
2 | 2 | =============================================== |
3 | 3 |
|
4 | | -Work in progress |
| 4 | +This bundle introduce some [Doctrine](https://github.com/doctrine/orm) features and helpers. |
| 5 | + |
| 6 | +`objects` is often a shortcut reference to `entities` or `documents`. |
| 7 | + |
| 8 | +EntityIterator and DocumentIterator |
| 9 | +--------------------------------------- |
| 10 | +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: |
| 11 | + |
| 12 | +ORM |
| 13 | +```php |
| 14 | +use App\Entity\User; |
| 15 | +use Doctrine\ORM\EntityManagerInterface; |
| 16 | +use Fazland\ApiPlatformBundle\Doctrine\ORM\EntityIterator; |
| 17 | + |
| 18 | +/** @var EntityManagerInterface $em */ |
| 19 | +$em = // The EntityManagerInterface instance. |
| 20 | +$qb = $em->createQueryBuilder(); |
| 21 | +$qb |
| 22 | + ->select('u') |
| 23 | + ->from(User::class, 'u') |
| 24 | +; |
| 25 | + |
| 26 | +$users = new EntityIterator($qb); |
| 27 | +foreach ($users as $user) { |
| 28 | + var_dump($user instanceof User); |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +MongoDB: |
| 33 | +```php |
| 34 | +use App\Document\User; |
| 35 | +use Doctrine\ODM\MongoDB\DocumentManager; |
| 36 | +use Fazland\ApiPlatformBundle\Doctrine\Mongo\DocumentIterator; |
| 37 | + |
| 38 | +/** @var DocumentManager $dm */ |
| 39 | +$dm = // The EntityManagerInterface instance. |
| 40 | +$qb = $dm->createQueryBuilder(User::class); |
| 41 | + |
| 42 | +$users = new DocumentIterator($qb); |
| 43 | +foreach ($users as $user) { |
| 44 | + var_dump($user instanceof User); |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +EntityRepository and DocumentRepository |
| 49 | +--------------------------------------- |
| 50 | +These two classes are just an extension of the base Doctrine Repository class with some utility methods: |
| 51 | +```php |
| 52 | +public function all(): ObjectIterator |
| 53 | +``` |
| 54 | +returns all objects matching that object. |
| 55 | + |
| 56 | +```php |
| 57 | +public function count(array $criteria = []): int |
| 58 | +``` |
| 59 | +returns an integer representing the count of all objects matching the given criteria. |
| 60 | + |
| 61 | +```php |
| 62 | +public function findOneByCached(array $criteria, array $orderBy = null, int $ttl = 28800) |
| 63 | +``` |
| 64 | +executes the base `findOneBy` and caches the results. |
| 65 | + |
| 66 | +```php |
| 67 | +public function findByCached(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null, int $ttl = 28800) |
| 68 | +``` |
| 69 | +executes the base `findBy` method and caches the results. |
| 70 | + |
| 71 | +```php |
| 72 | +public function get($id, $lockMode = null, $lockVersion = null) |
| 73 | +``` |
| 74 | +executes the base `find` method and throws a `NoResultException` if no result has been found. |
| 75 | + |
| 76 | +```php |
| 77 | +public function getOneBy(array $criteria, ?array $orderBy = null) |
| 78 | +``` |
| 79 | +executes the base `findOneBy` method and throws a `NoResultException` if no result has been found. |
| 80 | + |
| 81 | +```php |
| 82 | +public function getOneByCached(array $criteria, ?array $orderBy = null, int $ttl = 28800) |
| 83 | +``` |
| 84 | + |
| 85 | +executes the `findOneByCached` method and throws a `NoResultException` if no result has been found. |
| 86 | + |
| 87 | +Unfortunately, `Doctrine\ODM\MongoDB` does not support cache. |
| 88 | + |
| 89 | +How to use |
| 90 | +---------- |
| 91 | +Just set the `default_repository_class` in the Doctrine configuration: |
| 92 | + |
| 93 | +ORM |
| 94 | +```yaml |
| 95 | +doctrine: |
| 96 | + orm: |
| 97 | + default_repository_class: Fazland\ApiPlatformBundle\Doctrine\ORM\EntityRepository |
| 98 | +``` |
| 99 | +
|
| 100 | +MongoDB: |
| 101 | +```yaml |
| 102 | +doctrine_mongodb: |
| 103 | + document_managers: |
| 104 | + default_repository_class: Fazland\ApiPlatformBundle\Doctrine\Mongo\DocumentRepository |
| 105 | +``` |
0 commit comments