3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\DBAL\Schema;
22 use \Doctrine\DBAL\Types\Type;
23 use Doctrine\DBAL\Schema\Visitor\Visitor;
26 * Object representation of a database column
29 * @link www.doctrine-project.org
32 * @author Benjamin Eberlei <kontakt@beberlei.de>
34 class Column extends AbstractAsset
37 * @var \Doctrine\DBAL\Types\Type
44 protected $_length = null;
49 protected $_precision = 10;
54 protected $_scale = 0;
59 protected $_unsigned = false;
64 protected $_fixed = false;
69 protected $_notnull = true;
74 protected $_default = null;
79 protected $_autoincrement = false;
84 protected $_platformOptions = array();
89 protected $_columnDefinition = null;
94 protected $_comment = null;
99 protected $_customSchemaOptions = array();
102 * Create a new Column
104 * @param string $columnName
105 * @param \Doctrine\DBAL\Types\Type $type
107 * @param bool $notNull
108 * @param mixed $default
109 * @param bool $unsigned
111 * @param int $precision
113 * @param array $platformOptions
115 public function __construct($columnName, Type $type, array $options=array())
117 $this->_setName($columnName);
118 $this->setType($type);
119 $this->setOptions($options);
123 * @param array $options
126 public function setOptions(array $options)
128 foreach ($options as $name => $value) {
129 $method = "set".$name;
130 if (method_exists($this, $method)) {
131 $this->$method($value);
141 public function setType(Type $type)
143 $this->_type = $type;
151 public function setLength($length)
153 if($length !== null) {
154 $this->_length = (int)$length;
156 $this->_length = null;
162 * @param int $precision
165 public function setPrecision($precision)
167 if (!is_numeric($precision)) {
168 $precision = 10; // defaults to 10 when no valid precision is given.
171 $this->_precision = (int)$precision;
179 public function setScale($scale)
181 if (!is_numeric($scale)) {
185 $this->_scale = (int)$scale;
191 * @param bool $unsigned
194 public function setUnsigned($unsigned)
196 $this->_unsigned = (bool)$unsigned;
205 public function setFixed($fixed)
207 $this->_fixed = (bool)$fixed;
212 * @param bool $notnull
215 public function setNotnull($notnull)
217 $this->_notnull = (bool)$notnull;
223 * @param mixed $default
226 public function setDefault($default)
228 $this->_default = $default;
234 * @param array $platformOptions
237 public function setPlatformOptions(array $platformOptions)
239 $this->_platformOptions = $platformOptions;
245 * @param string $name
246 * @param mixed $value
249 public function setPlatformOption($name, $value)
251 $this->_platformOptions[$name] = $value;
260 public function setColumnDefinition($value)
262 $this->_columnDefinition = $value;
266 public function getType()
271 public function getLength()
273 return $this->_length;
276 public function getPrecision()
278 return $this->_precision;
281 public function getScale()
283 return $this->_scale;
286 public function getUnsigned()
288 return $this->_unsigned;
291 public function getFixed()
293 return $this->_fixed;
296 public function getNotnull()
298 return $this->_notnull;
301 public function getDefault()
303 return $this->_default;
306 public function getPlatformOptions()
308 return $this->_platformOptions;
311 public function hasPlatformOption($name)
313 return isset($this->_platformOptions[$name]);
316 public function getPlatformOption($name)
318 return $this->_platformOptions[$name];
321 public function getColumnDefinition()
323 return $this->_columnDefinition;
326 public function getAutoincrement()
328 return $this->_autoincrement;
331 public function setAutoincrement($flag)
333 $this->_autoincrement = $flag;
337 public function setComment($comment)
339 $this->_comment = $comment;
343 public function getComment()
345 return $this->_comment;
349 * @param string $name
350 * @param mixed $value
353 public function setCustomSchemaOption($name, $value)
355 $this->_customSchemaOptions[$name] = $value;
360 * @param string $name
363 public function hasCustomSchemaOption($name)
365 return isset($this->_customSchemaOptions[$name]);
369 * @param string $name
372 public function getCustomSchemaOption($name)
374 return $this->_customSchemaOptions[$name];
378 * @param array $customSchemaOptions
381 public function setCustomSchemaOptions(array $customSchemaOptions)
383 $this->_customSchemaOptions = $customSchemaOptions;
390 public function getCustomSchemaOptions()
392 return $this->_customSchemaOptions;
396 * @param Visitor $visitor
398 public function visit(\Doctrine\DBAL\Schema\Visitor $visitor)
400 $visitor->accept($this);
406 public function toArray()
408 return array_merge(array(
409 'name' => $this->_name,
410 'type' => $this->_type,
411 'default' => $this->_default,
412 'notnull' => $this->_notnull,
413 'length' => $this->_length,
414 'precision' => $this->_precision,
415 'scale' => $this->_scale,
416 'fixed' => $this->_fixed,
417 'unsigned' => $this->_unsigned,
418 'autoincrement' => $this->_autoincrement,
419 'columnDefinition' => $this->_columnDefinition,
420 'comment' => $this->_comment,
421 ), $this->_platformOptions, $this->_customSchemaOptions);