. */ namespace Doctrine\ORM\Mapping\Builder; /** * Field Builder * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.com * @since 2.2 * @author Benjamin Eberlei */ class FieldBuilder { /** * @var ClassMetadataBuilder */ private $builder; /** * @var array */ private $mapping; /** * @var bool */ private $version; /** * @var string */ private $generatedValue; /** * @var array */ private $sequenceDef; /** * @param ClassMetadataBuilder $builder * @param array $mapping */ public function __construct(ClassMetadataBuilder $builder, array $mapping) { $this->builder = $builder; $this->mapping = $mapping; } /** * Sets length. * * @param int $length * * @return FieldBuilder */ public function length($length) { $this->mapping['length'] = $length; return $this; } /** * Sets nullable. * * @param bool $flag * * @return FieldBuilder */ public function nullable($flag = true) { $this->mapping['nullable'] = (bool)$flag; return $this; } /** * Sets Unique. * * @param bool $flag * * @return FieldBuilder */ public function unique($flag = true) { $this->mapping['unique'] = (bool)$flag; return $this; } /** * Sets column name. * * @param string $name * * @return FieldBuilder */ public function columnName($name) { $this->mapping['columnName'] = $name; return $this; } /** * Sets Precision. * * @param int $p * * @return FieldBuilder */ public function precision($p) { $this->mapping['precision'] = $p; return $this; } /** * Sets scale. * * @param int $s * * @return FieldBuilder */ public function scale($s) { $this->mapping['scale'] = $s; return $this; } /** * Sets field as primary key. * * @return FieldBuilder */ public function isPrimaryKey() { $this->mapping['id'] = true; return $this; } /** * @param string $strategy * * @return FieldBuilder */ public function generatedValue($strategy = 'AUTO') { $this->generatedValue = $strategy; return $this; } /** * Sets field versioned. * * @return FieldBuilder */ public function isVersionField() { $this->version = true; return $this; } /** * Sets Sequence Generator. * * @param string $sequenceName * @param int $allocationSize * @param int $initialValue * * @return FieldBuilder */ public function setSequenceGenerator($sequenceName, $allocationSize = 1, $initialValue = 1) { $this->sequenceDef = array( 'sequenceName' => $sequenceName, 'allocationSize' => $allocationSize, 'initialValue' => $initialValue, ); return $this; } /** * Sets column definition. * * @param string $def * * @return FieldBuilder */ public function columnDefinition($def) { $this->mapping['columnDefinition'] = $def; return $this; } /** * Finalizes this field and attach it to the ClassMetadata. * * Without this call a FieldBuilder has no effect on the ClassMetadata. * * @return ClassMetadataBuilder */ public function build() { $cm = $this->builder->getClassMetadata(); if ($this->generatedValue) { $cm->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' . $this->generatedValue)); } if ($this->version) { $cm->setVersionMapping($this->mapping); } $cm->mapField($this->mapping); if ($this->sequenceDef) { $cm->setSequenceGeneratorDefinition($this->sequenceDef); } return $this->builder; } }