ColumnSchema.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db;
  8. use yii\base\Object;
  9. /**
  10. * ColumnSchema class describes the metadata of a column in a database table.
  11. *
  12. * @author Qiang Xue <[email protected]>
  13. * @since 2.0
  14. */
  15. class ColumnSchema extends Object
  16. {
  17. /**
  18. * @var string name of this column (without quotes).
  19. */
  20. public $name;
  21. /**
  22. * @var boolean whether this column can be null.
  23. */
  24. public $allowNull;
  25. /**
  26. * @var string abstract type of this column. Possible abstract types include:
  27. * string, text, boolean, smallint, integer, bigint, float, decimal, datetime,
  28. * timestamp, time, date, binary, and money.
  29. */
  30. public $type;
  31. /**
  32. * @var string the PHP type of this column. Possible PHP types include:
  33. * string, boolean, integer, double.
  34. */
  35. public $phpType;
  36. /**
  37. * @var string the DB type of this column. Possible DB types vary according to the type of DBMS.
  38. */
  39. public $dbType;
  40. /**
  41. * @var mixed default value of this column
  42. */
  43. public $defaultValue;
  44. /**
  45. * @var array enumerable values. This is set only if the column is declared to be an enumerable type.
  46. */
  47. public $enumValues;
  48. /**
  49. * @var integer display size of the column.
  50. */
  51. public $size;
  52. /**
  53. * @var integer precision of the column data, if it is numeric.
  54. */
  55. public $precision;
  56. /**
  57. * @var integer scale of the column data, if it is numeric.
  58. */
  59. public $scale;
  60. /**
  61. * @var boolean whether this column is a primary key
  62. */
  63. public $isPrimaryKey;
  64. /**
  65. * @var boolean whether this column is auto-incremental
  66. */
  67. public $autoIncrement = false;
  68. /**
  69. * @var boolean whether this column is unsigned. This is only meaningful
  70. * when [[type]] is `smallint`, `integer` or `bigint`.
  71. */
  72. public $unsigned;
  73. /**
  74. * @var string comment of this column. Not all DBMS support this.
  75. */
  76. public $comment;
  77. /**
  78. * Converts the input value according to [[phpType]].
  79. * If the value is null or an [[Expression]], it will not be converted.
  80. * @param mixed $value input value
  81. * @return mixed converted value
  82. */
  83. public function typecast($value)
  84. {
  85. if ($value === '' && $this->type !== Schema::TYPE_TEXT && $this->type !== Schema::TYPE_STRING && $this->type !== Schema::TYPE_BINARY) {
  86. return null;
  87. }
  88. if ($value === null || gettype($value) === $this->phpType || $value instanceof Expression) {
  89. return $value;
  90. }
  91. switch ($this->phpType) {
  92. case 'string':
  93. return (string)$value;
  94. case 'integer':
  95. return (integer)$value;
  96. case 'boolean':
  97. return (boolean)$value;
  98. }
  99. return $value;
  100. }
  101. }