consistence-community/consistence-doctrine-symfony
Composer 安装命令:
composer require consistence-community/consistence-doctrine-symfony
包简介
Symfony Bundle integrating Consistence library with Doctrine ORM
README 文档
README
This package is a fork of consistence/consistence-doctrine-symfony maintained by community to support new PHP versions.
This is a Symfony bundle providing integration for the standalone package
consistence-community/consistence-doctrine, if you are not using Symfony, follow instructions there.
This bundle 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 Users:
<?php namespace Consistence\Doctrine\Example\User; class Sex extends \Consistence\Enum\Enum { public const FEMALE = 'female'; public const MALE = 'male'; }
Now you can use the Sex enum in your User entity. There are two important things to notice:
type="string_enum"inORM\Column- this will be used for mapping the value to your database, that means if you have a string based enum (see values inSex), usestring_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.
@Enum(class=Sex::class)- this will be used for reconstructing theSexenum 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.
<?php namespace Consistence\Doctrine\Example\User; use Consistence\Doctrine\Enum\EnumAnnotation as Enum; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity() */ class User extends \Consistence\ObjectPrototype { // ... /** * @Enum(class=Sex::class) * @ORM\Column(type="string_enum", nullable=true) * @var \Consistence\Doctrine\Example\User\Sex|null */ private $sex; // ... public function __construct( // ... Sex $sex = null // ... ) { // ... $this->sex = $sex; // ... } // ... }
Now everything is ready to be used, when you call flush, only female will be saved:
<?php namespace Consistence\Doctrine\Example\User; $user = new User( // ... Sex::get(Sex::FEMALE) // ... ); /** @var \Doctrine\ORM\EntityManager $entityManager */ $entityManager->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 namespace Consistence\Doctrine\Example\User; /** @var \Doctrine\ORM\EntityManager $entityManager */ $user = $entityManager->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 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:
services: mycache: class: Doctrine\Common\Cache\FilesystemCache arguments: - '%kernel.cache_dir%/mycache' consistence.doctrine.enum.enum_fields_cache: '@mycache'
Installation
- Install package
consistence-community/consistence-doctrine-symfonywith Composer:
composer require consistence-community/consistence-doctrine-symfony
- Register the bundle in your application:
// config/bundles.php return [ // ... Consistence\Doctrine\SymfonyBundle\ConsistenceDoctrineBundle::class => ['all' => true], ];
That's all, you are good to go!
consistence-community/consistence-doctrine-symfony 适用场景与选型建议
consistence-community/consistence-doctrine-symfony 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 268.72k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 03 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「orm」 「symfony」 「bundle」 「doctrine」 「consistence」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 consistence-community/consistence-doctrine-symfony 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 consistence-community/consistence-doctrine-symfony 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 consistence-community/consistence-doctrine-symfony 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
2lenet/EasyAdminPlusBundle
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
The bundle for easy using json-rpc api on your project
Provide a way to secure accesses to all routes of an symfony application.
DMS Meetup API Bundle, enables Meetup API clients in services
统计信息
- 总下载量: 268.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-06