https://github.com/consistence/consistence-doctrine-symfony
Symfony Bundle integrating Consistence library with Doctrine ORM
https://github.com/consistence/consistence-doctrine-symfony
Last synced: 12 months ago
JSON representation
Symfony Bundle integrating Consistence library with Doctrine ORM
- Host: GitHub
- URL: https://github.com/consistence/consistence-doctrine-symfony
- Owner: consistence
- License: other
- Created: 2017-01-13T14:52:09.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2025-03-10T03:23:01.000Z (about 1 year ago)
- Last Synced: 2025-03-29T21:51:09.325Z (about 1 year ago)
- Language: PHP
- Homepage:
- Size: 99.6 KB
- Stars: 5
- Watchers: 2
- Forks: 8
- Open Issues: 8
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
Symfony Bundle integrating Consistence library with Doctrine ORM
================================================================
> This is a Symfony bundle providing integration for the standalone package
[`consistence/consistence-doctrine`](https://github.com/consistence/consistence-doctrine),
if you are not using Symfony, follow instructions there.
This bundle provides integration of [Consistence](https://github.com/consistence/consistence) value objects for [Doctrine ORM](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/) so that you can use them in your entities.
For now, the only integration which is needed is for [Enums](https://github.com/consistence/consistence/blob/master/docs/Enum/enums.md), see the examples below.
Usage
-----
[Enums](https://github.com/consistence/consistence/blob/master/docs/Enum/enums.md) represent predefined set of values and of course, you will want to store these values in your database as well. Since [`Enums`](https://github.com/consistence/consistence/blob/master/src/Enum/Enum.php) are objects and you only want to store the represented value, there has to be some mapping.
You can see it in this example where you want to store sex for your `User`s:
```php
sex = $sex;
// ...
}
// ...
}
```
Now everything is ready to be used, when you call `flush`, only `female` will be saved:
```php
persist($user);
// when persisting User::$sex to database, `female` will be saved
$entityManager->flush();
```
And when you retrieve the entity back from database, you will receive the `Sex` enum object again:
```php
find(User::class, 1);
var_dump($user->getSex());
/*
class Consistence\Doctrine\Example\User\Sex#5740 (1) {
private $value =>
string(6) "female"
}
*/
```
This means that the objects API is symmetrical (you get the same type as you set) and you can start benefiting from [Enums](https://github.com/consistence/consistence/blob/master/docs/Enum/enums.md) advantages such as being sure, that what you get is already a valid value and having the possibility to define methods on top of the represented values.
Configuration
-------------
You can override services used internally, for example if you want to use a more effective cache in production (which is recommended), you can provide custom instance with an [alias](http://symfony.com/doc/current/components/dependency_injection/advanced.html#aliasing):
```yaml
services:
mycache:
class: 'Doctrine\Common\Cache\FilesystemCache'
arguments:
$directory: '%kernel.cache_dir%/mycache'
consistence.doctrine.enum.enum_fields_cache: '@mycache'
```
Installation
------------
1) Install package [`consistence/consistence-doctrine-symfony`](https://packagist.org/packages/consistence/consistence-doctrine-symfony) with [Composer](https://getcomposer.org/):
```bash
composer require consistence/consistence-doctrine-symfony
```
2) Register the bundle in your application:
```php
// config/bundles.php
return [
// ...
Consistence\Doctrine\SymfonyBundle\ConsistenceDoctrineBundle::class => ['all' => true],
];
```
That's all, you are good to go!