{"id":21951444,"url":"https://github.com/consistence/consistence-doctrine","last_synced_at":"2025-04-10T03:57:26.325Z","repository":{"id":15834572,"uuid":"78843547","full_name":"consistence/consistence-doctrine","owner":"consistence","description":"Integration of Consistence library with Doctrine ORM","archived":false,"fork":false,"pushed_at":"2025-03-10T05:38:53.000Z","size":135,"stargazers_count":9,"open_issues_count":6,"forks_count":9,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T03:57:22.932Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/consistence.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-13T11:27:12.000Z","updated_at":"2025-03-07T07:48:26.000Z","dependencies_parsed_at":"2025-01-02T19:15:55.435Z","dependency_job_id":"24851554-3442-4d6a-b531-49126858c41d","html_url":"https://github.com/consistence/consistence-doctrine","commit_stats":{"total_commits":73,"total_committers":3,"mean_commits":"24.333333333333332","dds":0.04109589041095896,"last_synced_commit":"d66444a5666638dfe263b18357f5a880a9d2c1e4"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/consistence%2Fconsistence-doctrine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/consistence%2Fconsistence-doctrine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/consistence%2Fconsistence-doctrine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/consistence%2Fconsistence-doctrine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/consistence","download_url":"https://codeload.github.com/consistence/consistence-doctrine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248154996,"owners_count":21056542,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-29T06:14:14.230Z","updated_at":"2025-04-10T03:57:26.307Z","avatar_url":"https://github.com/consistence.png","language":"PHP","readme":"Integration of Consistence library with Doctrine ORM\n====================================================\n\nThis 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.\n\nFor 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.\n\nUsage\n-----\n\n[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.\n\nYou can see it in this example where you want to store sex for your `User`s:\n\n```php\n\u003c?php\n\nnamespace Consistence\\Doctrine\\Example\\User;\n\nclass Sex extends \\Consistence\\Enum\\Enum\n{\n\n\tpublic const FEMALE = 'female';\n\tpublic const MALE = 'male';\n\n}\n```\n\nNow you can use the `Sex` enum in your `User` entity. There are two important things to notice:\n\n1) `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`\n\nYou can specify any other parameters for `ORM\\Column` as you would usually (nullability, length...).\n\nThere is also `integer_enum`, `float_enum` and `boolean_enum` which can be used respectively for their types.\n\n2) `@Enum(class=Sex::class)` - this will be used for reconstructing the `Sex`\n enum object when loading the value back from database\n\nThe `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.\n\n```php\n\u003c?php\n\nnamespace Consistence\\Doctrine\\Example\\User;\n\nuse Consistence\\Doctrine\\Enum\\EnumAnnotation as Enum;\n\nuse Doctrine\\ORM\\Mapping as ORM;\n\n/**\n * @ORM\\Entity()\n */\nclass User extends \\Consistence\\ObjectPrototype\n{\n\n\t// ...\n\n\t/**\n\t * @Enum(class=Sex::class)\n\t * @ORM\\Column(type=\"string_enum\", nullable=true)\n\t * @var \\Consistence\\Doctrine\\Example\\User\\Sex|null\n\t */\n\tprivate $sex;\n\n\t// ...\n\n\tpublic function __construct(\n\t\t// ...\n\t\tSex $sex = null\n\t\t// ...\n\t)\n\t{\n\t\t// ...\n\t\t$this-\u003esex = $sex;\n\t\t// ...\n\t}\n\n\t// ...\n\n}\n```\n\nNow everything is ready to be used, when you call `flush`, only `female` will be saved:\n\n```php\n\u003c?php\n\nnamespace Consistence\\Doctrine\\Example\\User;\n\n$user = new User(\n\t// ...\n\tSex::get(Sex::FEMALE)\n\t// ...\n);\n/** @var \\Doctrine\\ORM\\EntityManager $entityManager */\n$entityManager-\u003epersist($user);\n\n// when persisting User::$sex to database, `female` will be saved\n$entityManager-\u003eflush();\n```\n\nAnd when you retrieve the entity back from database, you will receive the `Sex` enum object again:\n\n```php\n\u003c?php\n\nnamespace Consistence\\Doctrine\\Example\\User;\n\n/** @var \\Doctrine\\ORM\\EntityManager $entityManager */\n$user = $entityManager-\u003efind(User::class, 1);\nvar_dump($user-\u003egetSex());\n\n/*\n\nclass Consistence\\Doctrine\\Example\\User\\Sex#5740 (1) {\n  private $value =\u003e\n  string(6) \"female\"\n}\n\n*/\n```\n\nThis 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.\n\nInstallation\n------------\n\n\u003e 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.\n\n1) Install package [`consistence/consistence-doctrine`](https://packagist.org/packages/consistence/consistence-doctrine) with [Composer](https://getcomposer.org/):\n\n```bash\ncomposer require consistence/consistence-doctrine\n```\n\n2) 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):\n\n```php\n\u003c?php\n\nuse Consistence\\Doctrine\\Enum\\Type\\BooleanEnumType;\nuse Consistence\\Doctrine\\Enum\\Type\\FloatEnumType;\nuse Consistence\\Doctrine\\Enum\\Type\\IntegerEnumType;\nuse Consistence\\Doctrine\\Enum\\Type\\StringEnumType;\n\nuse Doctrine\\Common\\Annotations\\AnnotationRegistry;\nuse Doctrine\\DBAL\\Types\\Type as DoctrineType;\n\n// path to your Composer autoload file\n$loader = require __DIR__ . '/../vendor/autoload.php';\n\n// register loading of custom annotations\n// if you are already using Doctrine annotations you probably won't need this\nAnnotationRegistry::registerLoader([$loader, 'loadClass']);\n\n// register Doctrine DBAL types\nDoctrineType::addType(BooleanEnumType::NAME, BooleanEnumType::class); // boolean_enum\nDoctrineType::addType(FloatEnumType::NAME, FloatEnumType::class); // float_enum\nDoctrineType::addType(IntegerEnumType::NAME, IntegerEnumType::class); // integer_enum\nDoctrineType::addType(StringEnumType::NAME, StringEnumType::class); // string_enum\n```\n\nThis 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.\n\nIf you are already using [Doctrine annotations](http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/annotations.html), the `AnnotationRegistry::registerLoader()` might already be called somewhere in your application, so check that before adding it.\n\n3) [Register postLoad listener](http://doctrine-orm.readthedocs.io/en/latest/reference/events.html#listening-and-subscribing-to-lifecycle-events):\n\nYou need to register [`EnumPostLoadEntityListener`](/src/Enum/EnumPostLoadEntityListener.php), which needs `\\Doctrine\\Common\\Annotations\\Reader`. If you are using [annotations Doctrine mapping](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html), then you can use the same reader this way:\n\n```php\n\u003c?php\n\nuse Consistence\\Doctrine\\Enum\\EnumPostLoadEntityListener;\nuse Doctrine\\Common\\Cache\\ArrayCache;\nuse Doctrine\\ORM\\Events;\n\n/** @var \\Doctrine\\ORM\\EntityManager $entityManager */\n/** @var \\Doctrine\\ORM\\Mapping\\Driver\\AnnotationDriver $annotationDriver */\n$annotationDriver = $entityManager-\u003egetConfiguration()-\u003egetMetadataDriverImpl();\n$annotationReader = $annotationDriver-\u003egetReader();\n\n// make sure to use the most appropriate cache for given environment to get the best performance\n$cache = new ArrayCache();\n\n$entityManager-\u003egetEventManager()-\u003eaddEventListener(\n\tEvents::postLoad,\n\tnew EnumPostLoadEntityListener($annotationReader, $cache)\n);\n```\n\nIf not, just create a new instance and pass it to the constructor.\n\nThat's all, you are good to go!\n","funding_links":[],"categories":["Related Projects"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconsistence%2Fconsistence-doctrine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fconsistence%2Fconsistence-doctrine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconsistence%2Fconsistence-doctrine/lists"}