TableSchema.php 2.79 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 10
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.yiiframework.com/
 * @copyright Copyright &copy; 2008-2011 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
11 12
namespace yii\db\dao;

Qiang Xue committed
13 14
use yii\db\Exception;

w  
Qiang Xue committed
15
/**
Qiang Xue committed
16
 * TableSchema represents the metadata of a database table.
Qiang Xue committed
17 18
 *
 * @property array $columnNames list of column names
w  
Qiang Xue committed
19 20
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
21
 * @since 2.0
w  
Qiang Xue committed
22
 */
Qiang Xue committed
23
class TableSchema extends \yii\base\Object
w  
Qiang Xue committed
24
{
Qiang Xue committed
25 26 27 28 29 30
	/**
	 * @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
31 32 33 34
	/**
	 * @var string name of the schema that this table belongs to.
	 */
	public $schemaName;
w  
Qiang Xue committed
35 36 37 38 39
	/**
	 * @var string name of this table.
	 */
	public $name;
	/**
w  
Qiang Xue committed
40
	 * @var string quoted name of this table. This will include [[schemaName]] if it is not empty.
w  
Qiang Xue committed
41
	 */
w  
Qiang Xue committed
42
	public $quotedName;
w  
Qiang Xue committed
43
	/**
Qiang Xue committed
44
	 * @var array primary keys of this table.
w  
Qiang Xue committed
45
	 */
Qiang Xue committed
46
	public $primaryKey = array();
w  
Qiang Xue committed
47 48 49 50 51
	/**
	 * @var string sequence name for the primary key. Null if no sequence.
	 */
	public $sequenceName;
	/**
w  
Qiang Xue committed
52 53 54 55
	 * @var array foreign keys of this table. Each array element is of the following structure:
	 *
	 * ~~~
	 * array(
Qiang Xue committed
56 57 58
	 *	 'ForeignTableName',
	 *	 'fk1' => 'pk1',  // pk1 is in foreign table
	 *	 'fk2' => 'pk2',  // if composite foreign key
w  
Qiang Xue committed
59 60
	 * )
	 * ~~~
w  
Qiang Xue committed
61 62 63
	 */
	public $foreignKeys = array();
	/**
w  
Qiang Xue committed
64
	 * @var array column metadata of this table. Each array element is a [[ColumnSchema]] object, indexed by column names.
w  
Qiang Xue committed
65 66 67 68 69 70 71
	 */
	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
72
	 * @return ColumnSchema metadata of the named column. Null if the named column does not exist.
w  
Qiang Xue committed
73 74 75 76 77 78 79
	 */
	public function getColumn($name)
	{
		return isset($this->columns[$name]) ? $this->columns[$name] : null;
	}

	/**
Qiang Xue committed
80
	 * Returns the names of all columns in this table.
w  
Qiang Xue committed
81 82 83 84 85 86
	 * @return array list of column names
	 */
	public function getColumnNames()
	{
		return array_keys($this->columns);
	}
Qiang Xue committed
87

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