2011-09-04 16:13:20 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
2012-05-26 16:37:00 +04:00
|
|
|
* and is licensed under the MIT license. For more information, see
|
2011-09-04 16:13:20 +04:00
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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 <kontakt@beberlei.de>
|
|
|
|
*/
|
|
|
|
class FieldBuilder
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ClassMetadataBuilder
|
|
|
|
*/
|
|
|
|
private $builder;
|
2012-12-03 13:36:08 +04:00
|
|
|
|
2011-09-04 16:13:20 +04:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $mapping;
|
2012-12-03 13:36:08 +04:00
|
|
|
|
2011-09-04 16:13:20 +04:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $version;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $generatedValue;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $sequenceDef;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ClassMetadataBuilder $builder
|
2012-12-03 13:36:08 +04:00
|
|
|
* @param array $mapping
|
2011-09-04 16:13:20 +04:00
|
|
|
*/
|
|
|
|
public function __construct(ClassMetadataBuilder $builder, array $mapping)
|
|
|
|
{
|
|
|
|
$this->builder = $builder;
|
|
|
|
$this->mapping = $mapping;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-03 13:36:08 +04:00
|
|
|
* Sets length.
|
2011-09-04 16:13:20 +04:00
|
|
|
*
|
|
|
|
* @param int $length
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
2011-09-04 16:13:20 +04:00
|
|
|
* @return FieldBuilder
|
|
|
|
*/
|
|
|
|
public function length($length)
|
|
|
|
{
|
|
|
|
$this->mapping['length'] = $length;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-03 13:36:08 +04:00
|
|
|
* Sets nullable.
|
|
|
|
*
|
|
|
|
* @param bool $flag
|
2011-09-04 16:13:20 +04:00
|
|
|
*
|
|
|
|
* @return FieldBuilder
|
|
|
|
*/
|
|
|
|
public function nullable($flag = true)
|
|
|
|
{
|
|
|
|
$this->mapping['nullable'] = (bool)$flag;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-03 13:36:08 +04:00
|
|
|
* Sets Unique.
|
|
|
|
*
|
|
|
|
* @param bool $flag
|
2011-09-04 16:13:20 +04:00
|
|
|
*
|
|
|
|
* @return FieldBuilder
|
|
|
|
*/
|
|
|
|
public function unique($flag = true)
|
|
|
|
{
|
|
|
|
$this->mapping['unique'] = (bool)$flag;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-03 13:36:08 +04:00
|
|
|
* Sets column name.
|
2011-09-04 16:13:20 +04:00
|
|
|
*
|
|
|
|
* @param string $name
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
2011-09-04 16:13:20 +04:00
|
|
|
* @return FieldBuilder
|
|
|
|
*/
|
|
|
|
public function columnName($name)
|
|
|
|
{
|
|
|
|
$this->mapping['columnName'] = $name;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-03 13:36:08 +04:00
|
|
|
* Sets Precision.
|
|
|
|
*
|
|
|
|
* @param int $p
|
2011-09-04 16:13:20 +04:00
|
|
|
*
|
|
|
|
* @return FieldBuilder
|
|
|
|
*/
|
|
|
|
public function precision($p)
|
|
|
|
{
|
|
|
|
$this->mapping['precision'] = $p;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-03 13:36:08 +04:00
|
|
|
* Sets scale.
|
2011-09-04 16:13:20 +04:00
|
|
|
*
|
|
|
|
* @param int $s
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
2011-09-04 16:13:20 +04:00
|
|
|
* @return FieldBuilder
|
|
|
|
*/
|
|
|
|
public function scale($s)
|
|
|
|
{
|
|
|
|
$this->mapping['scale'] = $s;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-03 13:36:08 +04:00
|
|
|
* Sets field as primary key.
|
2011-09-04 16:13:20 +04:00
|
|
|
*
|
|
|
|
* @return FieldBuilder
|
|
|
|
*/
|
|
|
|
public function isPrimaryKey()
|
|
|
|
{
|
|
|
|
$this->mapping['id'] = true;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-03 13:36:08 +04:00
|
|
|
* @param string $strategy
|
|
|
|
*
|
2011-09-04 16:13:20 +04:00
|
|
|
* @return FieldBuilder
|
|
|
|
*/
|
|
|
|
public function generatedValue($strategy = 'AUTO')
|
|
|
|
{
|
|
|
|
$this->generatedValue = $strategy;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-03 13:36:08 +04:00
|
|
|
* Sets field versioned.
|
2011-09-04 16:13:20 +04:00
|
|
|
*
|
|
|
|
* @return FieldBuilder
|
|
|
|
*/
|
|
|
|
public function isVersionField()
|
|
|
|
{
|
|
|
|
$this->version = true;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-03 13:36:08 +04:00
|
|
|
* Sets Sequence Generator.
|
2011-12-20 01:56:19 +04:00
|
|
|
*
|
2011-09-04 16:13:20 +04:00
|
|
|
* @param string $sequenceName
|
2012-12-03 13:36:08 +04:00
|
|
|
* @param int $allocationSize
|
|
|
|
* @param int $initialValue
|
|
|
|
*
|
2011-09-04 16:13:20 +04:00
|
|
|
* @return FieldBuilder
|
|
|
|
*/
|
|
|
|
public function setSequenceGenerator($sequenceName, $allocationSize = 1, $initialValue = 1)
|
|
|
|
{
|
|
|
|
$this->sequenceDef = array(
|
|
|
|
'sequenceName' => $sequenceName,
|
|
|
|
'allocationSize' => $allocationSize,
|
|
|
|
'initialValue' => $initialValue,
|
|
|
|
);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-03 13:36:08 +04:00
|
|
|
* Sets column definition.
|
2011-12-20 01:56:19 +04:00
|
|
|
*
|
2011-09-04 16:13:20 +04:00
|
|
|
* @param string $def
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
2011-09-04 16:13:20 +04:00
|
|
|
* @return FieldBuilder
|
|
|
|
*/
|
|
|
|
public function columnDefinition($def)
|
|
|
|
{
|
|
|
|
$this->mapping['columnDefinition'] = $def;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-03 13:36:08 +04:00
|
|
|
* Finalizes this field and attach it to the ClassMetadata.
|
2011-09-04 16:13:20 +04:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
}
|
2012-05-26 16:37:00 +04:00
|
|
|
}
|