SchemaBuilderTrait.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. /**
  9. * SchemaBuilderTrait contains shortcut methods to create instances of [[ColumnSchemaBuilder]].
  10. *
  11. * These can be used in database migrations to define database schema types using a PHP interface.
  12. * This is useful to define a schema in a DBMS independent way so that the application may run on
  13. * different DBMS the same way.
  14. *
  15. * For example you may use the following code inside your migration files:
  16. *
  17. * ```php
  18. * $this->createTable('example_table', [
  19. * 'id' => $this->primaryKey(),
  20. * 'name' => $this->string(64)->notNull(),
  21. * 'type' => $this->integer()->notNull()->defaultValue(10),
  22. * 'description' => $this->text(),
  23. * 'rule_name' => $this->string(64),
  24. * 'data' => $this->text(),
  25. * 'created_at' => $this->datetime()->notNull(),
  26. * 'updated_at' => $this->datetime(),
  27. * ]);
  28. * ```
  29. *
  30. * @author Vasenin Matvey <vaseninm@gmail.com>
  31. * @since 2.0.6
  32. */
  33. trait SchemaBuilderTrait
  34. {
  35. /**
  36. * @return Connection the database connection to be used for schema building.
  37. */
  38. abstract protected function getDb();
  39. /**
  40. * Creates a primary key column.
  41. * @param int $length column size or precision definition.
  42. * This parameter will be ignored if not supported by the DBMS.
  43. * @return ColumnSchemaBuilder the column instance which can be further customized.
  44. * @since 2.0.6
  45. */
  46. public function primaryKey($length = null)
  47. {
  48. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_PK, $length);
  49. }
  50. /**
  51. * Creates a big primary key column.
  52. * @param int $length column size or precision definition.
  53. * This parameter will be ignored if not supported by the DBMS.
  54. * @return ColumnSchemaBuilder the column instance which can be further customized.
  55. * @since 2.0.6
  56. */
  57. public function bigPrimaryKey($length = null)
  58. {
  59. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_BIGPK, $length);
  60. }
  61. /**
  62. * Creates a char column.
  63. * @param int $length column size definition i.e. the maximum string length.
  64. * This parameter will be ignored if not supported by the DBMS.
  65. * @return ColumnSchemaBuilder the column instance which can be further customized.
  66. * @since 2.0.8
  67. */
  68. public function char($length = null)
  69. {
  70. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_CHAR, $length);
  71. }
  72. /**
  73. * Creates a string column.
  74. * @param int $length column size definition i.e. the maximum string length.
  75. * This parameter will be ignored if not supported by the DBMS.
  76. * @return ColumnSchemaBuilder the column instance which can be further customized.
  77. * @since 2.0.6
  78. */
  79. public function string($length = null)
  80. {
  81. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_STRING, $length);
  82. }
  83. /**
  84. * Creates a text column.
  85. * @return ColumnSchemaBuilder the column instance which can be further customized.
  86. * @since 2.0.6
  87. */
  88. public function text()
  89. {
  90. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_TEXT);
  91. }
  92. /**
  93. * Creates a tinyint column. If tinyint is not supported by the DBMS, smallint will be used.
  94. * @param int $length column size or precision definition.
  95. * This parameter will be ignored if not supported by the DBMS.
  96. * @return ColumnSchemaBuilder the column instance which can be further customized.
  97. * @since 2.0.14
  98. */
  99. public function tinyInteger($length = null)
  100. {
  101. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_TINYINT, $length);
  102. }
  103. /**
  104. * Creates a smallint column.
  105. * @param int $length column size or precision definition.
  106. * This parameter will be ignored if not supported by the DBMS.
  107. * @return ColumnSchemaBuilder the column instance which can be further customized.
  108. * @since 2.0.6
  109. */
  110. public function smallInteger($length = null)
  111. {
  112. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_SMALLINT, $length);
  113. }
  114. /**
  115. * Creates an integer column.
  116. * @param int $length column size or precision definition.
  117. * This parameter will be ignored if not supported by the DBMS.
  118. * @return ColumnSchemaBuilder the column instance which can be further customized.
  119. * @since 2.0.6
  120. */
  121. public function integer($length = null)
  122. {
  123. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_INTEGER, $length);
  124. }
  125. /**
  126. * Creates a bigint column.
  127. * @param int $length column size or precision definition.
  128. * This parameter will be ignored if not supported by the DBMS.
  129. * @return ColumnSchemaBuilder the column instance which can be further customized.
  130. * @since 2.0.6
  131. */
  132. public function bigInteger($length = null)
  133. {
  134. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_BIGINT, $length);
  135. }
  136. /**
  137. * Creates a float column.
  138. * @param int $precision column value precision. First parameter passed to the column type, e.g. FLOAT(precision).
  139. * This parameter will be ignored if not supported by the DBMS.
  140. * @return ColumnSchemaBuilder the column instance which can be further customized.
  141. * @since 2.0.6
  142. */
  143. public function float($precision = null)
  144. {
  145. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_FLOAT, $precision);
  146. }
  147. /**
  148. * Creates a double column.
  149. * @param int $precision column value precision. First parameter passed to the column type, e.g. DOUBLE(precision).
  150. * This parameter will be ignored if not supported by the DBMS.
  151. * @return ColumnSchemaBuilder the column instance which can be further customized.
  152. * @since 2.0.6
  153. */
  154. public function double($precision = null)
  155. {
  156. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_DOUBLE, $precision);
  157. }
  158. /**
  159. * Creates a decimal column.
  160. * @param int $precision column value precision, which is usually the total number of digits.
  161. * First parameter passed to the column type, e.g. DECIMAL(precision, scale).
  162. * This parameter will be ignored if not supported by the DBMS.
  163. * @param int $scale column value scale, which is usually the number of digits after the decimal point.
  164. * Second parameter passed to the column type, e.g. DECIMAL(precision, scale).
  165. * This parameter will be ignored if not supported by the DBMS.
  166. * @return ColumnSchemaBuilder the column instance which can be further customized.
  167. * @since 2.0.6
  168. */
  169. public function decimal($precision = null, $scale = null)
  170. {
  171. $length = [];
  172. if ($precision !== null) {
  173. $length[] = $precision;
  174. }
  175. if ($scale !== null) {
  176. $length[] = $scale;
  177. }
  178. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_DECIMAL, $length);
  179. }
  180. /**
  181. * Creates a datetime column.
  182. * @param int $precision column value precision. First parameter passed to the column type, e.g. DATETIME(precision).
  183. * This parameter will be ignored if not supported by the DBMS.
  184. * @return ColumnSchemaBuilder the column instance which can be further customized.
  185. * @since 2.0.6
  186. */
  187. public function dateTime($precision = null)
  188. {
  189. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_DATETIME, $precision);
  190. }
  191. /**
  192. * Creates a timestamp column.
  193. * @param int $precision column value precision. First parameter passed to the column type, e.g. TIMESTAMP(precision).
  194. * This parameter will be ignored if not supported by the DBMS.
  195. * @return ColumnSchemaBuilder the column instance which can be further customized.
  196. * @since 2.0.6
  197. */
  198. public function timestamp($precision = null)
  199. {
  200. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_TIMESTAMP, $precision);
  201. }
  202. /**
  203. * Creates a time column.
  204. * @param int $precision column value precision. First parameter passed to the column type, e.g. TIME(precision).
  205. * This parameter will be ignored if not supported by the DBMS.
  206. * @return ColumnSchemaBuilder the column instance which can be further customized.
  207. * @since 2.0.6
  208. */
  209. public function time($precision = null)
  210. {
  211. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_TIME, $precision);
  212. }
  213. /**
  214. * Creates a date column.
  215. * @return ColumnSchemaBuilder the column instance which can be further customized.
  216. * @since 2.0.6
  217. */
  218. public function date()
  219. {
  220. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_DATE);
  221. }
  222. /**
  223. * Creates a binary column.
  224. * @param int $length column size or precision definition.
  225. * This parameter will be ignored if not supported by the DBMS.
  226. * @return ColumnSchemaBuilder the column instance which can be further customized.
  227. * @since 2.0.6
  228. */
  229. public function binary($length = null)
  230. {
  231. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_BINARY, $length);
  232. }
  233. /**
  234. * Creates a boolean column.
  235. * @return ColumnSchemaBuilder the column instance which can be further customized.
  236. * @since 2.0.6
  237. */
  238. public function boolean()
  239. {
  240. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN);
  241. }
  242. /**
  243. * Creates a money column.
  244. * @param int $precision column value precision, which is usually the total number of digits.
  245. * First parameter passed to the column type, e.g. DECIMAL(precision, scale).
  246. * This parameter will be ignored if not supported by the DBMS.
  247. * @param int $scale column value scale, which is usually the number of digits after the decimal point.
  248. * Second parameter passed to the column type, e.g. DECIMAL(precision, scale).
  249. * This parameter will be ignored if not supported by the DBMS.
  250. * @return ColumnSchemaBuilder the column instance which can be further customized.
  251. * @since 2.0.6
  252. */
  253. public function money($precision = null, $scale = null)
  254. {
  255. $length = [];
  256. if ($precision !== null) {
  257. $length[] = $precision;
  258. }
  259. if ($scale !== null) {
  260. $length[] = $scale;
  261. }
  262. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_MONEY, $length);
  263. }
  264. /**
  265. * Creates a JSON column.
  266. * @return ColumnSchemaBuilder the column instance which can be further customized.
  267. * @since 2.0.14
  268. * @throws \yii\base\Exception
  269. */
  270. public function json()
  271. {
  272. /*
  273. * TODO Remove in Yii 2.1
  274. *
  275. * Disabled due to bug in MySQL extension
  276. * @link https://bugs.php.net/bug.php?id=70384
  277. */
  278. if (version_compare(PHP_VERSION, '5.6', '<') && $this->getDb()->getDriverName() === 'mysql') {
  279. throw new \yii\base\Exception('JSON column type is not supported in PHP < 5.6');
  280. }
  281. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_JSON);
  282. }
  283. }