ColumnSchema.php 3.7 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 29 30 31 32 33 34 35
    /**
     * @var string name of this column (without quotes).
     */
    public $name;
    /**
     * @var boolean whether this column can be null.
     */
    public $allowNull;
    /**
     * @var string abstract type of this column. Possible abstract types include:
     * string, text, boolean, smallint, integer, bigint, float, decimal, datetime,
     * timestamp, time, date, binary, and money.
     */
    public $type;
    /**
     * @var string the PHP type of this column. Possible PHP types include:
36
     * `string`, `boolean`, `integer`, `double`.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
     */
    public $phpType;
    /**
     * @var string the DB type of this column. Possible DB types vary according to the type of DBMS.
     */
    public $dbType;
    /**
     * @var mixed default value of this column
     */
    public $defaultValue;
    /**
     * @var array enumerable values. This is set only if the column is declared to be an enumerable type.
     */
    public $enumValues;
    /**
     * @var integer display size of the column.
     */
    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;
    /**
     * @var boolean whether this column is unsigned. This is only meaningful
     * when [[type]] is `smallint`, `integer` or `bigint`.
     */
    public $unsigned;
    /**
     * @var string comment of this column. Not all DBMS support this.
     */
    public $comment;
Qiang Xue committed
80

81

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

108 109
        return $value;
    }
110 111 112 113 114 115 116 117

    /**
     * Converts the input value according to [[type]] and [[dbType]] for use in a db query.
     * If the value is null or an [[Expression]], it will not be converted.
     * @param mixed $value input value
     * @return mixed converted value. This may also be an array containing the value as the first element
     * and the PDO type as the second element.
     */
118
    public function dbTypecast($value)
119 120 121
    {
        // the default implementation does the same as casting for PHP but it should be possible
        // to override this with annotation of explicit PDO type.
122
        return $this->phpTypecast($value);
123
    }
w  
Qiang Xue committed
124
}