Download the PHP package consistence/consistence-doctrine without Composer
On this page you can find all versions of the php package consistence/consistence-doctrine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download consistence/consistence-doctrine
More information about consistence/consistence-doctrine
Files in consistence/consistence-doctrine
Package consistence-doctrine
Short Description Integration of Consistence library with Doctrine ORM
License MIT
Informations about the package consistence-doctrine
Integration of Consistence library with Doctrine ORM
This library provides integration of Consistence value objects for Doctrine ORM so that you can use them in your entities.
For now, the only integration which is needed is for Enums, see the examples below.
Usage
Enums represent predefined set of values and of course, you will want to store these values in your database as well. Since Enums
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:
Now you can use the Sex
enum in your User
entity. There are two important things to notice:
1) type="string_enum"
in ORM\Column
- this will be used for mapping the value to your database, that means if you have a string based enum (see values in Sex
), use string_enum
You can specify any other parameters for ORM\Column
as you would usually (nullability, length...).
There is also integer_enum
, float_enum
and boolean_enum
which can be used respectively for their types.
2) @Enum(class=Sex::class)
- this will be used for reconstructing the Sex
enum object when loading the value back from database
The class
annotation parameter uses the same namespace resolution process as other Doctrine annotations, so it is practically the same as when you specify a targetEntity
in associations mapping.
Now everything is ready to be used, when you call flush
, only female
will be saved:
And when you retrieve the entity back from database, you will receive the Sex
enum object again:
This means that the objects API is symmetrical (you get the same type as you set) and you can start benefiting from Enums 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
, which will take care of the integration.
1) Install package consistence/consistence-doctrine
with Composer:
2) Register Doctrine DBAL types and annotations:
This step contains static call which have global effect, so I recommend putting them inside a bootstrap file (usually where you register the Composer autoloader now), which is run or included when the application starts.
If you are already using Doctrine annotations, the AnnotationRegistry::registerLoader()
might already be called somewhere in your application, so check that before adding it.
3) Register postLoad listener:
You need to register annotations Doctrine mapping, then you can use the same reader this way:
If not, just create a new instance and pass it to the constructor.
That's all, you are good to go!
All versions of consistence-doctrine with dependencies
consistence/consistence Version ~1.0|~2.0
doctrine/annotations Version ~1.2
doctrine/cache Version ^1.11
doctrine/orm Version ^2.6.3