consistence/consistence-doctrine

Consistence 库与 Doctrine ORM 集成

2.0.1 2022-03-18 09:32 UTC

README

此库提供了 Consistence 值对象与 Doctrine ORM 的集成,以便您可以在实体中使用它们。

目前,需要的唯一集成是 枚举,下面是示例。

使用方法

枚举表示预定义值集,当然,您也希望将这些值存储在数据库中。由于 枚举 是对象,并且您只想存储代表值,因此需要进行一些映射。

您可以在下面的示例中看到,您想为 User 存储性别

<?php

namespace Consistence\Doctrine\Example\User;

class Sex extends \Consistence\Enum\Enum
{

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

}

现在您可以在 User 实体中使用 Sex 枚举。有两个重要事项需要注意

  1. ORM\Column 中的 type="string_enum" - 这将用于将值映射到您的数据库,这意味着如果您有一个基于字符串的枚举(参见 Sex 中的值),请使用 string_enum

您可以为 ORM\Column 指定任何其他参数,就像您通常做的那样(nullability、长度...)。

还有 integer_enumfloat_enumboolean_enum,分别用于它们的类型。

  1. @Enum(class=Sex::class) - 这将用于从数据库中重新加载值时重建 Sex 枚举对象

class 注释参数使用与 Doctrine 注释相同的命名空间解析过程,因此在关联映射中指定 targetEntity 时实际上是相同的。

<?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;
		// ...
	}

	// ...

}

现在一切准备就绪,当您调用 flush 时,只有 female 将被保存

<?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();

当您从数据库中检索实体时,您将再次收到 Sex 枚举对象

<?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"
}

*/

这意味着对象的 API 是对称的(您得到与您设置的相同类型)并且您可以开始从 枚举 的优势中受益,例如确保您获得的是有效值,并且有机会在表示的值之上定义方法。

安装

如果您正在使用 Symfony,您可以使用 consistence/consistence-doctrine-symfony,它将处理集成。

  1. 使用 Composer 安装包 consistence/consistence-doctrine
composer require consistence/consistence-doctrine
  1. 注册 Doctrine DBAL 类型注释
<?php

use Consistence\Doctrine\Enum\Type\BooleanEnumType;
use Consistence\Doctrine\Enum\Type\FloatEnumType;
use Consistence\Doctrine\Enum\Type\IntegerEnumType;
use Consistence\Doctrine\Enum\Type\StringEnumType;

use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\DBAL\Types\Type as DoctrineType;

// path to your Composer autoload file
$loader = require __DIR__ . '/../vendor/autoload.php';

// register loading of custom annotations
// if you are already using Doctrine annotations you probably won't need this
AnnotationRegistry::registerLoader([$loader, 'loadClass']);

// register Doctrine DBAL types
DoctrineType::addType(BooleanEnumType::NAME, BooleanEnumType::class); // boolean_enum
DoctrineType::addType(FloatEnumType::NAME, FloatEnumType::class); // float_enum
DoctrineType::addType(IntegerEnumType::NAME, IntegerEnumType::class); // integer_enum
DoctrineType::addType(StringEnumType::NAME, StringEnumType::class); // string_enum

此步骤包含具有全局影响的静态调用,因此我建议将其放在引导文件中(通常是现在注册Composer自动加载器的地方),在应用程序启动时运行或包含。

如果您已经在使用Doctrine注解,那么AnnotationRegistry::registerLoader()可能已经在您的应用程序的某个地方被调用,所以在添加它之前请检查。

  1. 注册postLoad监听器:

您需要注册EnumPostLoadEntityListener,它需要\Doctrine\Common\Annotations\Reader。如果您正在使用注解Doctrine映射,那么您可以通过这种方式使用相同的读取器

<?php

use Consistence\Doctrine\Enum\EnumPostLoadEntityListener;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\ORM\Events;

/** @var \Doctrine\ORM\EntityManager $entityManager */
/** @var \Doctrine\ORM\Mapping\Driver\AnnotationDriver $annotationDriver */
$annotationDriver = $entityManager->getConfiguration()->getMetadataDriverImpl();
$annotationReader = $annotationDriver->getReader();

// make sure to use the most appropriate cache for given environment to get the best performance
$cache = new ArrayCache();

$entityManager->getEventManager()->addEventListener(
	Events::postLoad,
	new EnumPostLoadEntityListener($annotationReader, $cache)
);

如果没有,只需创建一个新实例并将其传递给构造函数。

就是这样,您可以继续了!