{"id":21951435,"url":"https://github.com/consistence/consistence-doctrine-symfony","last_synced_at":"2025-04-23T03:53:16.548Z","repository":{"id":15840382,"uuid":"78858203","full_name":"consistence/consistence-doctrine-symfony","owner":"consistence","description":"Symfony Bundle integrating Consistence library with Doctrine ORM","archived":false,"fork":false,"pushed_at":"2025-03-10T03:23:01.000Z","size":102,"stargazers_count":5,"open_issues_count":8,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T21:51:09.325Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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-13T14:52:09.000Z","updated_at":"2025-03-07T11:24:16.000Z","dependencies_parsed_at":"2025-01-31T20:45:27.213Z","dependency_job_id":null,"html_url":"https://github.com/consistence/consistence-doctrine-symfony","commit_stats":{"total_commits":63,"total_committers":2,"mean_commits":31.5,"dds":"0.015873015873015928","last_synced_commit":"eeb545dfc2805e7dd343017538ba04500ca1b1b8"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/consistence%2Fconsistence-doctrine-symfony","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/consistence%2Fconsistence-doctrine-symfony/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/consistence%2Fconsistence-doctrine-symfony/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/consistence%2Fconsistence-doctrine-symfony/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/consistence","download_url":"https://codeload.github.com/consistence/consistence-doctrine-symfony/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250366695,"owners_count":21418768,"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:09.922Z","updated_at":"2025-04-23T03:53:16.528Z","avatar_url":"https://github.com/consistence.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Symfony Bundle integrating Consistence library with Doctrine ORM\n================================================================\n\n\u003e This is a Symfony bundle providing integration for the standalone package\n[`consistence/consistence-doctrine`](https://github.com/consistence/consistence-doctrine),\nif you are not using Symfony, follow instructions there.\n\nThis 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.\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\nConfiguration\n-------------\n\nYou 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):\n\n```yaml\nservices:\n    mycache:\n        class: 'Doctrine\\Common\\Cache\\FilesystemCache'\n        arguments:\n            $directory: '%kernel.cache_dir%/mycache'\n\n    consistence.doctrine.enum.enum_fields_cache: '@mycache'\n```\n\nInstallation\n------------\n\n1) Install package [`consistence/consistence-doctrine-symfony`](https://packagist.org/packages/consistence/consistence-doctrine-symfony) with [Composer](https://getcomposer.org/):\n\n```bash\ncomposer require consistence/consistence-doctrine-symfony\n```\n\n2) Register the bundle in your application:\n\n```php\n// config/bundles.php\nreturn [\n\t// ...\n\tConsistence\\Doctrine\\SymfonyBundle\\ConsistenceDoctrineBundle::class =\u003e ['all' =\u003e true],\n];\n```\n\nThat's all, you are good to go!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconsistence%2Fconsistence-doctrine-symfony","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fconsistence%2Fconsistence-doctrine-symfony","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconsistence%2Fconsistence-doctrine-symfony/lists"}