|
1 | 1 | # Data providers
|
2 | 2 |
|
3 | 3 | To retrieve data that will be exposed by the API, DunglasApiBundle use classes called data providers. A data provider
|
4 |
| -using Doctrine ORM to retrieve data from a database is included with the bundle and enabled by default. This data provider |
| 4 | +using [Doctrine ORM](http://www.doctrine-project.org/projects/orm.html) to retrieve data from a database is included with the bundle and enabled by default. This data provider |
5 | 5 | natively supports paged collection and filters. It can be used as is and fits perfectly with common usages.
|
6 | 6 |
|
7 | 7 | But sometime, you want to retrieve data from other sources such as a webservice, ElasticSearch, MongoDB or another ORM.
|
@@ -85,5 +85,56 @@ To create your own paginators, take a look at the Doctrine ORM paginator bridge:
|
85 | 85 | To be able [to filter collections](filters.md), the Data Provider must be aware of registered filters to the given resource.
|
86 | 86 | The best way to learn how to create filter aware data provider is too look at the default Doctrine ORM dataprovider: [`Dunglas\ApiBundle\Doctrine\Orm\DataProvider`](/Doctrine/Orm/DataProvider.php).
|
87 | 87 |
|
| 88 | +## Extending the Doctrine Data Provider |
| 89 | + |
| 90 | +The bundle is provided with a data provider leveraging the Doctrine ORM. This default data provider can be extended. |
| 91 | + |
| 92 | +For performance reasons, [custom output walkers for the Doctrine ORM Paginator](http://www.doctrine-project.org/jira/browse/DDC-3282) |
| 93 | +are disabled. It drastically improves performance when dealing with large collections. However it prevents advanced [filters](filters.md) |
| 94 | +adding `HAVING` and `GROUP BY` clauses to DQL queries to work properly. |
| 95 | + |
| 96 | +To enable custom output walkers, start by creating a custom data provider supporting the `AppBundle\Entity\MyEntity` class: |
| 97 | + |
| 98 | +```php |
| 99 | +<?php |
| 100 | +
|
| 101 | +// src/AppBundle/DataProvider/MyEntityDataProvider.php |
| 102 | +
|
| 103 | +namespace AppBundle\DataProvider; |
| 104 | +
|
| 105 | +use Dunglas\ApiBundle\Doctrine\Orm\DataProvider; |
| 106 | +use Dunglas\ApiBundle\Model\DataProviderInterface; |
| 107 | +
|
| 108 | +class MyEntityDataProvider extends DataProvider |
| 109 | +{ |
| 110 | + protected function getPaginator(QueryBuilder $queryBuilder) |
| 111 | + { |
| 112 | + $doctrineOrmPaginator = new DoctrineOrmPaginator($queryBuilder); |
| 113 | + // Enable output walkers to make queries with HAVING and ORDER BY clauses working |
| 114 | + $doctrineOrmPaginator->setUseOutputWalkers(true); |
| 115 | +
|
| 116 | + return new Paginator($doctrineOrmPaginator); |
| 117 | + } |
| 118 | + |
| 119 | + public function supports(ResourceInterface $resource) |
| 120 | + { |
| 121 | + return 'AppBundle\Entity\MyEntity' === $resource->getEntityClass(); |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +Then register the data provider: |
| 127 | + |
| 128 | +```yaml |
| 129 | +
|
| 130 | +# app/config/services.yml |
| 131 | +
|
| 132 | +services: |
| 133 | + my_entity_data_provider: |
| 134 | + parent: "api.doctrine.orm.data_provider" |
| 135 | + class: AppBundle\DataProvider\MyEntityDataProvider |
| 136 | + tags: [ { name: "api.data_provider", priority: 1 } ] |
| 137 | +``` |
| 138 | + |
88 | 139 | Previous chapter: [Operations](operations.md)<br>
|
89 | 140 | Next chapter: [Filters](filters.md)
|
0 commit comments