consistence/consistence-doctrine 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

consistence/consistence-doctrine

Composer 安装命令:

composer require consistence/consistence-doctrine

包简介

Integration of Consistence library with Doctrine ORM

README 文档

README

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 Users:

<?php

namespace Consistence\Doctrine\Example\User;

class Sex extends \Consistence\Enum\Enum
{

	public const FEMALE = 'female';
	public const MALE = 'male';

}

To use the Sex enum in your User entity notice the type="enum<Consistence\Doctrine\Example\User\Sex>" in ORM\Column - this will be used for mapping the value to and from your database. You can specify any other parameters for ORM\Column as you would usually (nullability, length...).

<?php

namespace Consistence\Doctrine\Example\User;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 */
class User extends \Consistence\ObjectPrototype
{

	// ...

	/**
	 * @ORM\Column(type="enum<Consistence\Doctrine\Example\User\Sex>", nullable=true)
	 * @var \Consistence\Doctrine\Example\User\Sex|null
	 */
	private $sex;

	// ...

	public function __construct(
		// ...
		Sex $sex = null
		// ...
	)
	{
		// ...
		$this->sex = $sex;
		// ...
	}

	// ...

}

Before this will work with database, we need to register Doctrine DBAL type for each enum.

If you are using Symfony, you can use consistence/consistence-doctrine-symfony, where you can just configure which types should be registered.

If you do this in code, note that this step contains static calls which have global effect. 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.

<?php

use Consistence\Doctrine\Enum\Type\StringEnumType;
use Consistence\Doctrine\Example\User\Sex;
use Doctrine\DBAL\Types\Type as DoctrineType;

foreach ([
	StringEnumType::create(Sex::class),
] as $type) {
	DoctrineType::getTypeRegistry()->register($type->getName(), $type);
}

We used StringEnumType because the Sex enum uses strings for its values. There are also IntegerEnumType, FloatEnumType and BooleanEnumType - depending on what scalar values are used in the enum.

If you use the same enum in multiple places register it only once.

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.

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:
composer require consistence/consistence-doctrine
  1. Register needed DBAL types (see Usage section above).

That's all, you are good to go!

consistence/consistence-doctrine 适用场景与选型建议

consistence/consistence-doctrine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 494.23k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2017 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 consistence/consistence-doctrine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 consistence/consistence-doctrine 我们能提供哪些服务?
定制开发 / 二次开发

基于 consistence/consistence-doctrine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 494.23k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 11
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 3
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-13