ColumnSchema.php 2.55 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
8
namespace yii\db;
w  
Qiang Xue committed
9

Qiang Xue committed
10 11
use yii\base\Object;

w  
Qiang Xue committed
12
/**
Qiang Xue committed
13
 * ColumnSchema class describes the metadata of a column in a database table.
w  
Qiang Xue committed
14 15
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
16
 * @since 2.0
w  
Qiang Xue committed
17
 */
Qiang Xue committed
18
class ColumnSchema extends Object
w  
Qiang Xue committed
19 20 21 22 23 24 25 26 27 28
{
	/**
	 * @var string name of this column (without quotes).
	 */
	public $name;
	/**
	 * @var boolean whether this column can be null.
	 */
	public $allowNull;
	/**
Qiang Xue committed
29
	 * @var string abstract type of this column. Possible abstract types include:
Qiang Xue committed
30 31
	 * string, text, boolean, smallint, integer, bigint, float, decimal, datetime,
	 * timestamp, time, date, binary, and money.
w  
Qiang Xue committed
32
	 */
w  
Qiang Xue committed
33
	public $type;
w  
Qiang Xue committed
34
	/**
w  
Qiang Xue committed
35 36
	 * @var string the PHP type of this column. Possible PHP types include:
	 * string, boolean, integer, double.
w  
Qiang Xue committed
37
	 */
w  
Qiang Xue committed
38 39
	public $phpType;
	/**
Qiang Xue committed
40
	 * @var string the DB type of this column. Possible DB types vary according to the type of DBMS.
w  
Qiang Xue committed
41 42
	 */
	public $dbType;
w  
Qiang Xue committed
43 44 45 46
	/**
	 * @var mixed default value of this column
	 */
	public $defaultValue;
w  
Qiang Xue committed
47
	/**
Qiang Xue committed
48
	 * @var array enumerable values. This is set only if the column is declared to be an enumerable type.
w  
Qiang Xue committed
49 50
	 */
	public $enumValues;
w  
Qiang Xue committed
51
	/**
Qiang Xue committed
52
	 * @var integer display size of the column.
w  
Qiang Xue committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	 */
	public $size;
	/**
	 * @var integer precision of the column data, if it is numeric.
	 */
	public $precision;
	/**
	 * @var integer scale of the column data, if it is numeric.
	 */
	public $scale;
	/**
	 * @var boolean whether this column is a primary key
	 */
	public $isPrimaryKey;
	/**
	 * @var boolean whether this column is auto-incremental
	 */
	public $autoIncrement = false;
	/**
w  
Qiang Xue committed
72
	 * @var boolean whether this column is unsigned. This is only meaningful
Qiang Xue committed
73
	 * when [[type]] is `smallint`, `integer` or `bigint`.
w  
Qiang Xue committed
74
	 */
w  
Qiang Xue committed
75
	public $unsigned;
w  
Qiang Xue committed
76
	/**
Qiang Xue committed
77
	 * @var string comment of this column. Not all DBMS support this.
w  
Qiang Xue committed
78
	 */
Qiang Xue committed
79 80
	public $comment;

w  
Qiang Xue committed
81 82

	/**
Qiang Xue committed
83 84
	 * Converts the input value according to [[phpType]].
	 * If the value is null or an [[Expression]], it will not be converted.
w  
Qiang Xue committed
85 86 87 88 89
	 * @param mixed $value input value
	 * @return mixed converted value
	 */
	public function typecast($value)
	{
w  
Qiang Xue committed
90
		if ($value === null || gettype($value) === $this->phpType || $value instanceof Expression) {
w  
Qiang Xue committed
91
			return $value;
w  
Qiang Xue committed
92
		}
93 94 95
		if ($value === '' && $this->type !== Schema::TYPE_TEXT && $this->type !== Schema::TYPE_STRING) {
			return null;
		}
w  
Qiang Xue committed
96
		switch ($this->phpType) {
Qiang Xue committed
97 98 99 100 101 102
			case 'string':
				return (string)$value;
			case 'integer':
				return (integer)$value;
			case 'boolean':
				return (boolean)$value;
w  
Qiang Xue committed
103
		}
w  
Qiang Xue committed
104
		return $value;
w  
Qiang Xue committed
105 106
	}
}