TableSchema.php 2.78 KB
Newer Older
w  
Qiang Xue committed
1 2 3 4 5 6 7
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright &copy; 2008-2011 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
8
namespace yii\db;
w  
Qiang Xue committed
9

Qiang Xue committed
10
use yii\base\Object;
Qiang Xue committed
11
use yii\base\InvalidParamException;
Qiang Xue committed
12

w  
Qiang Xue committed
13
/**
Qiang Xue committed
14
 * TableSchema represents the metadata of a database table.
Qiang Xue committed
15
 *
16
 * @property array $columnNames List of column names. This property is read-only.
w  
Qiang Xue committed
17 18
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
19
 * @since 2.0
w  
Qiang Xue committed
20
 */
Qiang Xue committed
21
class TableSchema extends Object
w  
Qiang Xue committed
22
{
w  
Qiang Xue committed
23
	/**
24
	 * @var string the name of the schema that this table belongs to.
w  
Qiang Xue committed
25 26
	 */
	public $schemaName;
w  
Qiang Xue committed
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.
w  
Qiang Xue committed
29 30
	 */
	public $name;
31 32 33 34 35 36
	/**
	 * @var string the full name of this table, which includes the schema name prefix, if any.
	 * Note that if the schema name is the same as the [[Schema::defaultSchema|default schema name]],
	 * the schema name will not be included.
	 */
	public $fullName;
w  
Qiang Xue committed
37
	/**
Qiang Xue committed
38
	 * @var string[] primary keys of this table.
w  
Qiang Xue committed
39
	 */
Alexander Makarov committed
40
	public $primaryKey = [];
w  
Qiang Xue committed
41 42 43 44 45
	/**
	 * @var string sequence name for the primary key. Null if no sequence.
	 */
	public $sequenceName;
	/**
w  
Qiang Xue committed
46 47 48
	 * @var array foreign keys of this table. Each array element is of the following structure:
	 *
	 * ~~~
Alexander Makarov committed
49
	 * [
Qiang Xue committed
50 51 52
	 *	 'ForeignTableName',
	 *	 'fk1' => 'pk1',  // pk1 is in foreign table
	 *	 'fk2' => 'pk2',  // if composite foreign key
Alexander Makarov committed
53
	 * ]
w  
Qiang Xue committed
54
	 * ~~~
w  
Qiang Xue committed
55
	 */
Alexander Makarov committed
56
	public $foreignKeys = [];
w  
Qiang Xue committed
57
	/**
Qiang Xue committed
58
	 * @var ColumnSchema[] column metadata of this table. Each array element is a [[ColumnSchema]] object, indexed by column names.
w  
Qiang Xue committed
59
	 */
Alexander Makarov committed
60
	public $columns = [];
w  
Qiang Xue committed
61

62

w  
Qiang Xue committed
63 64 65 66
	/**
	 * Gets the named column metadata.
	 * This is a convenient method for retrieving a named column even if it does not exist.
	 * @param string $name column name
Qiang Xue committed
67
	 * @return ColumnSchema metadata of the named column. Null if the named column does not exist.
w  
Qiang Xue committed
68 69 70 71 72 73 74
	 */
	public function getColumn($name)
	{
		return isset($this->columns[$name]) ? $this->columns[$name] : null;
	}

	/**
Qiang Xue committed
75
	 * Returns the names of all columns in this table.
w  
Qiang Xue committed
76 77 78 79 80 81
	 * @return array list of column names
	 */
	public function getColumnNames()
	{
		return array_keys($this->columns);
	}
Qiang Xue committed
82

Qiang Xue committed
83 84 85
	/**
	 * Manually specifies the primary key for this table.
	 * @param string|array $keys the primary key (can be composite)
Qiang Xue committed
86
	 * @throws InvalidParamException if the specified key cannot be found in the table.
Qiang Xue committed
87
	 */
Qiang Xue committed
88 89 90
	public function fixPrimaryKey($keys)
	{
		if (!is_array($keys)) {
Alexander Makarov committed
91
			$keys = [$keys];
Qiang Xue committed
92
		}
Qiang Xue committed
93 94 95 96
		$this->primaryKey = $keys;
		foreach ($this->columns as $column) {
			$column->isPrimaryKey = false;
		}
Qiang Xue committed
97 98 99 100
		foreach ($keys as $key) {
			if (isset($this->columns[$key])) {
				$this->columns[$key]->isPrimaryKey = true;
			} else {
Qiang Xue committed
101
				throw new InvalidParamException("Primary key '$key' cannot be found in table '{$this->name}'.");
Qiang Xue committed
102 103 104
			}
		}
	}
w  
Qiang Xue committed
105
}