|
| 1 | +# DoctrineOrmServiceFactory |
| 2 | + |
| 3 | +The *DoctrineOrmServiceFactory* provides integration with the [Doctrine ORM][1]. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +```sh |
| 8 | +composer require doctrine/orm "^2.5" |
| 9 | +``` |
| 10 | + |
| 11 | +## Parameters |
| 12 | + |
| 13 | +* **doctrine.orm.em.options**: Array of Doctrine ORM options. |
| 14 | + |
| 15 | + These options are available: |
| 16 | + |
| 17 | + * **cache.hydration**: Array with the cache settings, defaults to `['type' => 'array']`. |
| 18 | + Can be any of: `apcu`, `array`. Add additional cache provider factories by adding new service: |
| 19 | + `$container['doctrine.dbal.db.cache_factory.<type>']` |
| 20 | + * **cache.metadata**: Array with the cache settings, defaults to `['type' => 'array']`. |
| 21 | + Can be any of: `apcu`, `array`. Add additional cache provider factories by adding new service: |
| 22 | + `$container['doctrine.dbal.db.cache_factory.<type>']` |
| 23 | + * **cache.query**: Array with the cache settings, defaults to `['type' => 'array']`. |
| 24 | + Can be any of: `apcu`, `array`. Add additional cache provider factories by adding new service: |
| 25 | + `$container['doctrine.dbal.db.cache_factory.<type>']` |
| 26 | + * **class_metadata.factory.name**: String with class, defaults to `Doctrine\ORM\Mapping\ClassMetadataFactory`. |
| 27 | + * **connection**: The connection name of the Doctrine DBAL configuration. Defaults to `default`. |
| 28 | + * **custom.functions.datetime**: Array of datetime related [custom functions][2]. |
| 29 | + * **custom.functions.numeric**: Array of numeric related [custom functions][2]. |
| 30 | + * **custom.functions.string**: Array of string related [custom functions][2]. |
| 31 | + * **custom.hydration_modes**: Array of [hydration modes][3]. |
| 32 | + * **entity.listener_resolver**: String with the resolver type, defaults to `default`. |
| 33 | + Add additional resolvers by adding new service: |
| 34 | + `$container['doctrine.orm.entity.listener_resolver.<type>']`. |
| 35 | + * **mappings**: Array of Mappings. |
| 36 | + * **type**: The mapping driver to use. |
| 37 | + Can be any of: `annotation`, `class_map`, `php`, `simple_yaml`, `simple_xml`, `static_php`, `yaml` or `xml`. |
| 38 | + Add additional mapping driver factories by adding new service: |
| 39 | + `$container['doctrine.orm.mapping_driver.factory.<type>']` |
| 40 | + * **namespace**: The entity namespace. Example: `One\Entity` |
| 41 | + * **path**: The path to the entities. Example: `/path/to/project/One/Entity` |
| 42 | + * **alias**: The entity alias to the namespace. Example: `Alias\Entity` |
| 43 | + * **extension**: The file extension to search for mappings. Example: `.dcm.xml` |
| 44 | + * **proxies.auto_generate**: Enable or disable the auto generation of proxies. Defaults to `true`. |
| 45 | + * **proxies.dir**: The directory where generated proxies get saved. Example: `var/cache/doctrine/orm/proxies`. |
| 46 | + * **proxies.namespace**: The namespace of generated proxies. Defaults to `DoctrineProxy`. |
| 47 | + * **query_hints**: Array of [query hints][4]. |
| 48 | + * **repository.default.class**: String with class, defaults to `Doctrine\ORM\EntityRepository`. |
| 49 | + * **repository.factory**: String with the repository factory type, defaults to `default`. |
| 50 | + Add additional repository factories by adding new service: `$container['doctrine.orm.repository.factory.<type>']`. |
| 51 | + * **second_level_cache.enabled**: Enable or disable second level cache, defaults to `false`. |
| 52 | + * **second_level_cache**: Array with the cache settings, defaults to `['type' => 'array']`. |
| 53 | + Can be any of: `apcu`, `array`. Add additional cache provider factories by adding new service: |
| 54 | + `$container['doctrine.dbal.db.cache_factory.<type>']` |
| 55 | + * **strategy.naming**: String with the naming strategy type, defaults to `default`. |
| 56 | + Add additional naming stratigies by adding new service: `$container['doctrine.orm.strategy.naming.<type>']`. |
| 57 | + * **strategy.quote**: String with the quote strategy type, defaults to `default`. |
| 58 | + Add additional quote stratigies by adding new service: `$container['doctrine.orm.strategy.quote.<type>']`. |
| 59 | + |
| 60 | +## Services |
| 61 | + |
| 62 | +* **doctrine.orm.em**: The entity manager, instance of `Doctrine\ORM\EntityManager`. |
| 63 | +* **doctrine.orm.em.config**: Configuration object for Doctrine. Defaults to an empty `Doctrine\ORM\Configuration`. |
| 64 | +* **doctrine.orm.manager_registry**: The manager registry, instance of `Doctrine\Common\Persistence\ManagerRegistry`. |
| 65 | + |
| 66 | +## Registering |
| 67 | + |
| 68 | +### Single connection |
| 69 | + |
| 70 | +```php |
| 71 | +$container = new Container(); |
| 72 | + |
| 73 | +$container->factories((new Chubbyphp\DoctrineDbServiceProvider\ServiceFactory\DoctrineDbalServiceFactory())()); |
| 74 | +$container->factories((new Chubbyphp\DoctrineDbServiceProvider\ServiceFactory\DoctrineOrmServiceFactory())()); |
| 75 | + |
| 76 | +$container->factory('doctrine.dbal.db.options', static function () { |
| 77 | + return [ |
| 78 | + 'connection' => [ |
| 79 | + 'driver' => 'pdo_mysql', |
| 80 | + 'host' => 'mysql.someplace.tld', |
| 81 | + 'dbname' => 'my_database', |
| 82 | + 'user' => 'my_username', |
| 83 | + 'password' => 'my_password', |
| 84 | + 'charset' => 'utf8mb4', |
| 85 | + ], |
| 86 | + ]; |
| 87 | +}); |
| 88 | + |
| 89 | +$container->factory('doctrine.orm.em.options', static function () { |
| 90 | + return [ |
| 91 | + 'mappings' => [ |
| 92 | + [ |
| 93 | + 'type' => 'annotation', |
| 94 | + 'namespace' => 'One\Entity', |
| 95 | + 'path' => __DIR__.'/src/One/Entity', |
| 96 | + ] |
| 97 | + ], |
| 98 | + ]; |
| 99 | +}); |
| 100 | +``` |
| 101 | + |
| 102 | +### Multiple connections |
| 103 | + |
| 104 | +```php |
| 105 | +$container = new Container(); |
| 106 | + |
| 107 | +$container->factories((new Chubbyphp\DoctrineDbServiceProvider\ServiceFactory\DoctrineDbalServiceFactory())()); |
| 108 | +$container->factories((new Chubbyphp\DoctrineDbServiceProvider\ServiceFactory\DoctrineOrmServiceFactory())()); |
| 109 | + |
| 110 | +$container->factory('doctrine.dbal.dbs.options', static function () { |
| 111 | + return [ |
| 112 | + 'mysql_read' => [ |
| 113 | + 'connection' => [ |
| 114 | + 'driver' => 'pdo_mysql', |
| 115 | + 'host' => 'mysql_read.someplace.tld', |
| 116 | + 'dbname' => 'my_database', |
| 117 | + 'user' => 'my_username', |
| 118 | + 'password' => 'my_password', |
| 119 | + 'charset' => 'utf8mb4', |
| 120 | + ], |
| 121 | + ], |
| 122 | + 'mysql_write' => [ |
| 123 | + 'connection' => [ |
| 124 | + 'driver' => 'pdo_mysql', |
| 125 | + 'host' => 'mysql_write.someplace.tld', |
| 126 | + 'dbname' => 'my_database', |
| 127 | + 'user' => 'my_username', |
| 128 | + 'password' => 'my_password', |
| 129 | + 'charset' => 'utf8mb4', |
| 130 | + ], |
| 131 | + ], |
| 132 | + ]; |
| 133 | +}); |
| 134 | + |
| 135 | +$container->factory('doctrine.orm.ems.options', static function () { |
| 136 | + return [ |
| 137 | + 'mysql_read' => [ |
| 138 | + 'connection' => 'mysql_read', |
| 139 | + 'mappings' => [ |
| 140 | + [ |
| 141 | + 'type' => 'annotation', |
| 142 | + 'namespace' => 'One\Entity', |
| 143 | + 'alias' => 'One', |
| 144 | + 'path' => __DIR__.'/src/One/Entity', |
| 145 | + 'use_simple_annotation_reader' => false, |
| 146 | + ], |
| 147 | + ], |
| 148 | + ], |
| 149 | + 'mysql_write' => [ |
| 150 | + 'connection' => 'mysql_write', |
| 151 | + 'mappings' => [ |
| 152 | + [ |
| 153 | + 'type' => 'annotation', |
| 154 | + 'namespace' => 'One\Entity', |
| 155 | + 'path' => __DIR__.'/src/One/Entity', |
| 156 | + 'use_simple_annotation_reader' => false, |
| 157 | + ], |
| 158 | + ], |
| 159 | + ], |
| 160 | + ]; |
| 161 | +}); |
| 162 | +``` |
| 163 | + |
| 164 | +## Usage |
| 165 | + |
| 166 | +### Single connection |
| 167 | + |
| 168 | +```php |
| 169 | +$container->get('doctrine.orm.em') |
| 170 | + ->getRepository(User::class) |
| 171 | + ->findOneBy(['username' => 'john.doe@domain.com']); |
| 172 | +``` |
| 173 | + |
| 174 | +### Multiple connections |
| 175 | + |
| 176 | +```php |
| 177 | +$container->get('doctrine.orm.ems')->get('name') |
| 178 | + ->getRepository(User::class) |
| 179 | + ->findOneBy(['username' => 'john.doe@domain.com']); |
| 180 | +``` |
| 181 | + |
| 182 | +## Copyright |
| 183 | + |
| 184 | +Dominik Zogg <dominik.zogg@gmail.com> |
| 185 | + |
| 186 | +[1]: https://www.doctrine-project.org/projects/orm |
| 187 | +[2]: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/dql-user-defined-functions.html |
| 188 | +[3]: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#custom-hydration-modes |
| 189 | +[4]: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#query-hints |
| 190 | + |
0 commit comments