porkchopsandwiches/doctrine-utilities

一些通用的实用类,适合与 Doctrine 一起使用。

1.1.1 2015-03-11 01:28 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:59:15 UTC


README

基于 Doctrine 的项目通用工具,包括 UTCDateTime 列类型和基本的实体类。

实体管理器生成器

Doctrine 实体管理器的便利生成器,在 cli-config.php 和应用本身中都很有用。

自动注册 utcdatetime 列类型,无论 MySQL 服务器或 PHP 环境的时区如何,都存储 UTC 时间的 DateTime。

use PorkChopSandwiches\Doctrine\Utilities\EntityManager\Generator;

$entity_manager = Generator::manufacture(
	
	# The \Doctrine\DBAL\Connection instance or array
	$database_config, 
	
	# A \Doctrine\Common\Cache instance, for Query and MetaData caching
	$cache,
	
	# A \Doctrine\Common\Annotations\AnnotationReader instance
	$annotation_reader,
	
	# An array of directories to read Entity annotations from
	$entity_paths,
	
	# The proxy autogeneration behaviour
	AbstractProxyFactory::AUTOGENERATE_ALWAYS,
	
	# Whether to ensure production settings are on
	false,
	
	# Absolute path to Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php file
	ROOT_PATH . Generator::DOCTRINE_ANNOTATIONS_FILE_PATH,
	
	# PHP namespace for proxies
	"App\\Proxies",
	
	# Absolute path to directory where proxies will be generated
	ROOT_PATH . "/App/Proxies"
);

实体类

包括一个基本的 Entity 类,以及 3 个扩展的实用类

  1. DatedEntity,具有 date_createddate_updated UTC DateTime 列,
  2. AutoIncrementedIDEntity,具有自增的 UNSIGNED INT id 列,和
  3. DatedAutoIncrementedIDEntity,具有上述两者。

基本实体

use PorkChopSandwiches\Doctrine\Utilities\Entities\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(
 * 	name="sample_entities",
 *	uniqueConstraints={
 *	}
 * )
 */
class SampleEntity extends Entity {

	/**
	 * @ORM\Column(type="string", length=100, options={"default"=""})
	 * @ORM\Id
	 */
	private $label = "";

	/**
	 * @param array [$args]
	 * 
	 * @return array
	 */
	public function preserialise (array $args = array()) {
		return array(
			"label" => $this -> label
		);
	}
}

日期实体

use PorkChopSandwiches\Doctrine\Utilities\Entities\DatedEntity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(
 * 	name="sample_dated_entities",
 *	uniqueConstraints={
 *	}
 * )
 */
class SampleDatedEntity extends DatedEntity {

	/**
	 * @ORM\Column(type="string", length=100, options={"default"=""})
	 * @ORM\Id
	 */
	private $label = "";

	/**
	 * @param array [$args]
	 * 
	 * @return array
	 */
	public function preserialise (array $args = array()) {
		return array_merge(parent::preserialise($args), array(
			"label" => $this -> label
		));
	}
}

...

$instance = new SampleDatedEntity;
$instance -> getDateCreated(); // => DateTime
$instance -> getDateUpdated(); // => DateTime
$instance -> setDateUpdated(new \DateTime);

生成 Doctrine SQL

CREATE TABLE sample_dated_entities (`label` VARCHAR(100) DEFAULT '' NOT NULL, date_created DATETIME NOT NULL, date_updated DATETIME NOT NULL, PRIMARY KEY(`label`)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;

自增 ID 实体

具有自增 id UNSIGNED INT 列的实体。

use PorkChopSandwiches\Doctrine\Utilities\Entities\AutoIncrementedIDEntity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(
 * 	name="sample_aiid_entities",
 *	uniqueConstraints={
 *	}
 * )
 */
class SampleAutoIncrementedIDEntity extends AutoIncrementedIDEntity {

	/**
	 * @ORM\Column(type="string", length=100, options={"default"=""})
	 */
	private $label = "";

	public function preserialise (array $args = array()) {
		return array_merge(parent::preserialise($args), array(
			"label" => $this -> label
		));
	}
}

...

$instance = new SampleAutoIncrementedIDEntity;
$instance -> getID(); // int (or null if not yet flushed)

生成 Doctrine SQL

CREATE TABLE sample_aiid_entities (id INT UNSIGNED AUTO_INCREMENT NOT NULL, `label` VARCHAR(100) DEFAULT '' NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;