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
2.6 KiB
104 lines
2.6 KiB
14 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright © 2008-2011 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
12 years ago
|
namespace yii\db;
|
||
14 years ago
|
|
||
12 years ago
|
use yii\base\InvalidParamException;
|
||
13 years ago
|
|
||
14 years ago
|
/**
|
||
13 years ago
|
* TableSchema represents the metadata of a database table.
|
||
13 years ago
|
*
|
||
|
* @property array $columnNames list of column names
|
||
14 years ago
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
14 years ago
|
* @since 2.0
|
||
14 years ago
|
*/
|
||
13 years ago
|
class TableSchema extends \yii\base\Object
|
||
14 years ago
|
{
|
||
|
/**
|
||
13 years ago
|
* @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;
|
||
|
/**
|
||
14 years ago
|
* @var string name of the schema that this table belongs to.
|
||
|
*/
|
||
|
public $schemaName;
|
||
|
/**
|
||
14 years ago
|
* @var string name of this table.
|
||
|
*/
|
||
|
public $name;
|
||
|
/**
|
||
13 years ago
|
* @var string[] primary keys of this table.
|
||
14 years ago
|
*/
|
||
13 years ago
|
public $primaryKey = array();
|
||
14 years ago
|
/**
|
||
|
* @var string sequence name for the primary key. Null if no sequence.
|
||
|
*/
|
||
|
public $sequenceName;
|
||
|
/**
|
||
14 years ago
|
* @var array foreign keys of this table. Each array element is of the following structure:
|
||
|
*
|
||
|
* ~~~
|
||
|
* array(
|
||
13 years ago
|
* 'ForeignTableName',
|
||
|
* 'fk1' => 'pk1', // pk1 is in foreign table
|
||
|
* 'fk2' => 'pk2', // if composite foreign key
|
||
14 years ago
|
* )
|
||
|
* ~~~
|
||
14 years ago
|
*/
|
||
|
public $foreignKeys = array();
|
||
|
/**
|
||
13 years ago
|
* @var ColumnSchema[] column metadata of this table. Each array element is a [[ColumnSchema]] object, indexed by column names.
|
||
14 years ago
|
*/
|
||
|
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
|
||
13 years ago
|
* @return ColumnSchema metadata of the named column. Null if the named column does not exist.
|
||
14 years ago
|
*/
|
||
|
public function getColumn($name)
|
||
|
{
|
||
|
return isset($this->columns[$name]) ? $this->columns[$name] : null;
|
||
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* Returns the names of all columns in this table.
|
||
14 years ago
|
* @return array list of column names
|
||
|
*/
|
||
|
public function getColumnNames()
|
||
|
{
|
||
|
return array_keys($this->columns);
|
||
|
}
|
||
13 years ago
|
|
||
13 years ago
|
/**
|
||
|
* Manually specifies the primary key for this table.
|
||
|
* @param string|array $keys the primary key (can be composite)
|
||
12 years ago
|
* @throws InvalidParamException if the specified key cannot be found in the table.
|
||
13 years ago
|
*/
|
||
13 years ago
|
public function fixPrimaryKey($keys)
|
||
|
{
|
||
|
if (!is_array($keys)) {
|
||
|
$keys = array($keys);
|
||
|
}
|
||
13 years ago
|
$this->primaryKey = $keys;
|
||
|
foreach ($this->columns as $column) {
|
||
|
$column->isPrimaryKey = false;
|
||
|
}
|
||
13 years ago
|
foreach ($keys as $key) {
|
||
|
if (isset($this->columns[$key])) {
|
||
|
$this->columns[$key]->isPrimaryKey = true;
|
||
|
} else {
|
||
12 years ago
|
throw new InvalidParamException("Primary key '$key' cannot be found in table '{$this->name}'.");
|
||
13 years ago
|
}
|
||
|
}
|
||
|
}
|
||
14 years ago
|
}
|