You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.0KB

  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. use yii\base\InvalidParamException;
  10. /**
  11. * TableSchema represents the metadata of a database table.
  12. *
  13. * @property array $columnNames List of column names. This property is read-only.
  14. *
  15. * @author Qiang Xue <qiang.xue@gmail.com>
  16. * @since 2.0
  17. */
  18. class TableSchema extends Object
  19. {
  20. /**
  21. * @var string the name of the schema that this table belongs to.
  22. */
  23. public $schemaName;
  24. /**
  25. * @var string the name of this table. The schema name is not included. Use [[fullName]] to get the name with schema name prefix.
  26. */
  27. public $name;
  28. /**
  29. * @var string the full name of this table, which includes the schema name prefix, if any.
  30. * Note that if the schema name is the same as the [[Schema::defaultSchema|default schema name]],
  31. * the schema name will not be included.
  32. */
  33. public $fullName;
  34. /**
  35. * @var string[] primary keys of this table.
  36. */
  37. public $primaryKey = [];
  38. /**
  39. * @var string sequence name for the primary key. Null if no sequence.
  40. */
  41. public $sequenceName;
  42. /**
  43. * @var array foreign keys of this table. Each array element is of the following structure:
  44. *
  45. * ```php
  46. * [
  47. * 'ForeignTableName',
  48. * 'fk1' => 'pk1', // pk1 is in foreign table
  49. * 'fk2' => 'pk2', // if composite foreign key
  50. * ]
  51. * ```
  52. */
  53. public $foreignKeys = [];
  54. /**
  55. * @var ColumnSchema[] column metadata of this table. Each array element is a [[ColumnSchema]] object, indexed by column names.
  56. */
  57. public $columns = [];
  58. /**
  59. * Gets the named column metadata.
  60. * This is a convenient method for retrieving a named column even if it does not exist.
  61. * @param string $name column name
  62. * @return ColumnSchema metadata of the named column. Null if the named column does not exist.
  63. */
  64. public function getColumn($name)
  65. {
  66. return isset($this->columns[$name]) ? $this->columns[$name] : null;
  67. }
  68. /**
  69. * Returns the names of all columns in this table.
  70. * @return array list of column names
  71. */
  72. public function getColumnNames()
  73. {
  74. return array_keys($this->columns);
  75. }
  76. /**
  77. * Manually specifies the primary key for this table.
  78. * @param string|array $keys the primary key (can be composite)
  79. * @throws InvalidParamException if the specified key cannot be found in the table.
  80. */
  81. public function fixPrimaryKey($keys)
  82. {
  83. $keys = (array) $keys;
  84. $this->primaryKey = $keys;
  85. foreach ($this->columns as $column) {
  86. $column->isPrimaryKey = false;
  87. }
  88. foreach ($keys as $key) {
  89. if (isset($this->columns[$key])) {
  90. $this->columns[$key]->isPrimaryKey = true;
  91. } else {
  92. throw new InvalidParamException("Primary key '$key' cannot be found in table '{$this->name}'.");
  93. }
  94. }
  95. }
  96. }