選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ColumnSchema.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <qiang.xue@gmail.com>
  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. * char, 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]] after retrieval from the database.
  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 phpTypecast($value)
  84. {
  85. return $this->typecast($value);
  86. }
  87. /**
  88. * Converts the input value according to [[type]] and [[dbType]] for use in a db query.
  89. * If the value is null or an [[Expression]], it will not be converted.
  90. * @param mixed $value input value
  91. * @return mixed converted value. This may also be an array containing the value as the first element
  92. * and the PDO type as the second element.
  93. */
  94. public function dbTypecast($value)
  95. {
  96. // the default implementation does the same as casting for PHP, but it should be possible
  97. // to override this with annotation of explicit PDO type.
  98. return $this->typecast($value);
  99. }
  100. /**
  101. * Converts the input value according to [[phpType]] after retrieval from the database.
  102. * If the value is null or an [[Expression]], it will not be converted.
  103. * @param mixed $value input value
  104. * @return mixed converted value
  105. * @since 2.0.3
  106. */
  107. protected function typecast($value)
  108. {
  109. if ($value === '' && $this->type !== Schema::TYPE_TEXT && $this->type !== Schema::TYPE_STRING && $this->type !== Schema::TYPE_BINARY && $this->type !== Schema::TYPE_CHAR) {
  110. return null;
  111. }
  112. if ($value === null || gettype($value) === $this->phpType || $value instanceof Expression) {
  113. return $value;
  114. }
  115. switch ($this->phpType) {
  116. case 'resource':
  117. case 'string':
  118. if (is_resource($value)) {
  119. return $value;
  120. }
  121. if (is_float($value)) {
  122. // ensure type cast always has . as decimal separator in all locales
  123. return str_replace(',', '.', (string) $value);
  124. }
  125. return (string) $value;
  126. case 'integer':
  127. return (int) $value;
  128. case 'boolean':
  129. // treating a 0 bit value as false too
  130. // https://github.com/yiisoft/yii2/issues/9006
  131. return (bool) $value && $value !== "\0";
  132. case 'double':
  133. return (double) $value;
  134. }
  135. return $value;
  136. }
  137. }