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

Qiang Xue committed
10
namespace yii\db;
w  
Qiang Xue committed
11

Qiang Xue committed
12
use yii\base\InvalidCallException;
Qiang Xue committed
13

w  
Qiang Xue committed
14
/**
Qiang Xue committed
15
 * TableSchema represents the metadata of a database table.
Qiang Xue committed
16 17
 *
 * @property array $columnNames list of column names
w  
Qiang Xue committed
18 19
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
20
 * @since 2.0
w  
Qiang Xue committed
21
 */
Qiang Xue committed
22
class TableSchema extends \yii\base\Object
w  
Qiang Xue committed
23
{
Qiang Xue committed
24 25 26 27 28 29
	/**
	 * @var string name of the catalog (database) that this table belongs to.
	 * Defaults to null, meaning no catalog (or the current database).
	 * This property is only meaningful for MSSQL.
	 */
	public $catalogName;
w  
Qiang Xue committed
30 31 32 33
	/**
	 * @var string name of the schema that this table belongs to.
	 */
	public $schemaName;
w  
Qiang Xue committed
34 35 36 37 38
	/**
	 * @var string name of this table.
	 */
	public $name;
	/**
Qiang Xue committed
39
	 * @var string[] primary keys of this table.
w  
Qiang Xue committed
40
	 */
Qiang Xue committed
41
	public $primaryKey = array();
w  
Qiang Xue committed
42 43 44 45 46
	/**
	 * @var string sequence name for the primary key. Null if no sequence.
	 */
	public $sequenceName;
	/**
w  
Qiang Xue committed
47 48 49 50
	 * @var array foreign keys of this table. Each array element is of the following structure:
	 *
	 * ~~~
	 * array(
Qiang Xue committed
51 52 53
	 *	 'ForeignTableName',
	 *	 'fk1' => 'pk1',  // pk1 is in foreign table
	 *	 'fk2' => 'pk2',  // if composite foreign key
w  
Qiang Xue committed
54 55
	 * )
	 * ~~~
w  
Qiang Xue committed
56 57 58
	 */
	public $foreignKeys = array();
	/**
Qiang Xue committed
59
	 * @var ColumnSchema[] column metadata of this table. Each array element is a [[ColumnSchema]] object, indexed by column names.
w  
Qiang Xue committed
60 61 62 63 64 65 66
	 */
	public $columns = array();

	/**
	 * 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 InvalidCallException if the specified key cannot be found in the table.
Qiang Xue committed
87
	 */
Qiang Xue committed
88 89 90 91 92
	public function fixPrimaryKey($keys)
	{
		if (!is_array($keys)) {
			$keys = array($keys);
		}
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 InvalidCallException("Primary key '$key' cannot be found in table '{$this->name}'.");
Qiang Xue committed
102 103 104
			}
		}
	}
w  
Qiang Xue committed
105
}