porkchopsandwiches / doctrine-utilities
Some general utility classes suitable for use with Doctrine.
1.1.1
2015-03-11 01:28 UTC
Requires
- php: >=5.5
- doctrine/orm: ~2.4
- porkchopsandwiches/preserialiser: ~1.0
Requires (Dev)
- phpunit/phpunit: 4.5.*
This package is not auto-updated.
Last update: 2024-04-13 14:13:14 UTC
README
Common utilities for Doctrine-based projects, including a UTCDateTime column type and basic Entity classes.
Entity Manager Generator
Convenience generator for Doctrine Entity Manager, useful in both cli-config.php
and in the app proper.
Automatically registers the utcdatetime
column type which stores a DateTime in UTC regardless of the timezone of the MySQL server or PHP environment.
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 classes
Includes a basic Entity
class, plus 3 extending utility classes:
DatedEntity
, withdate_created
anddate_updated
UTC DateTime columns,AutoIncrementedIDEntity
with an auto-incrementing UNSIGNED INTid
column, andDatedAutoIncrementedIDEntity
with both of the above.
Basic entity
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 ); } }
Dated entity
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);
Produces 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;
Auto-incrementing ID entity
An entity with an auto-incrementing id
UNSIGNED INT column.
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)
Produces 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;