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