Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/consistence/consistence-doctrine

Integration of Consistence library with Doctrine ORM
https://github.com/consistence/consistence-doctrine

Last synced: 2 months ago
JSON representation

Integration of Consistence library with Doctrine ORM

Lists

README

        

Integration of Consistence library with Doctrine ORM
====================================================

This library 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.

Installation
------------

> If you are using Symfony, you can use [`consistence/consistence-doctrine-symfony`](https://github.com/consistence/consistence-doctrine-symfony), which will take care of the integration.

1) Install package [`consistence/consistence-doctrine`](https://packagist.org/packages/consistence/consistence-doctrine) with [Composer](https://getcomposer.org/):

```bash
composer require consistence/consistence-doctrine
```

2) Register [Doctrine DBAL types](http://doctrine-orm.readthedocs.org/en/latest/cookbook/custom-mapping-types.html) and [annotations](http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/annotations.html#registering-annotations):

```php
getConfiguration()->getMetadataDriverImpl();
$annotationReader = $annotationDriver->getReader();

// make sure to use the most appropriate cache for given environment to get the best performance
$cache = new ArrayCache();

$entityManager->getEventManager()->addEventListener(
Events::postLoad,
new EnumPostLoadEntityListener($annotationReader, $cache)
);
```

If not, just create a new instance and pass it to the constructor.

That's all, you are good to go!