類別 yii\db\TableSchema
繼承 | yii\db\TableSchema » yii\base\BaseObject |
---|---|
實作介面 | yii\base\Configurable |
子類別 | yii\db\mssql\TableSchema |
起始版本 | 2.0 |
原始碼 | https://github.com/yiisoft/yii2/blob/master/framework/db/TableSchema.php |
TableSchema 代表資料庫表格的元數據。
公開屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$columnNames | array | 欄位名稱列表。 | yii\db\TableSchema |
$columns | yii\db\ColumnSchema[] | 此表格的欄位元數據。 | yii\db\TableSchema |
$foreignKeys | array | 此表格的外鍵。 | yii\db\TableSchema |
$fullName | string | 此表格的完整名稱,包含任何的架構名稱前綴。 | yii\db\TableSchema |
$name | string | 此表格的名稱。 | yii\db\TableSchema |
$primaryKey | string[] | 此表格的主鍵。 | yii\db\TableSchema |
$schemaName | string | 此表格所屬的架構名稱。 | yii\db\TableSchema |
$sequenceName | string|null | 主鍵的序號名稱。 | yii\db\TableSchema |
公開方法
方法 | 描述 | 定義於 |
---|---|---|
__call() | 呼叫未定義為類別方法的具名方法。 | yii\base\BaseObject |
__construct() | 建構子。 | yii\base\BaseObject |
__get() | 回傳物件屬性的值。 | yii\base\BaseObject |
__isset() | 檢查屬性是否已設定,亦即已定義且非 null。 | yii\base\BaseObject |
__set() | 設定物件屬性的值。 | yii\base\BaseObject |
__unset() | 將物件屬性設定為 null。 | yii\base\BaseObject |
canGetProperty() | 回傳一個值,指出屬性是否可讀取。 | yii\base\BaseObject |
canSetProperty() | 回傳一個值,指出屬性是否可設定。 | yii\base\BaseObject |
className() | 回傳此類別的完整限定名稱。 | yii\base\BaseObject |
fixPrimaryKey() | 手動指定此表格的主鍵。 | yii\db\TableSchema |
getColumn() | 取得具名的欄位元數據。 | yii\db\TableSchema |
getColumnNames() | 回傳此表格中所有欄位的名稱。 | yii\db\TableSchema |
hasMethod() | 回傳一個值,指出方法是否已定義。 | yii\base\BaseObject |
hasProperty() | 回傳一個值,指出屬性是否已定義。 | yii\base\BaseObject |
init() | 初始化物件。 | yii\base\BaseObject |
屬性詳細資訊
此表格的欄位元數據。每個陣列元素都是一個 yii\db\ColumnSchema 物件,以欄位名稱索引。
此表格的外鍵。每個陣列元素都具有以下結構
[
'ForeignTableName',
'fk1' => 'pk1', // pk1 is in foreign table
'fk2' => 'pk2', // if composite foreign key
]
方法詳細資訊
public mixed __call ( $name, $params ) | ||
$name | string |
方法名稱 |
$params | array |
方法參數 |
return | mixed |
方法回傳值 |
---|---|---|
throws | yii\base\UnknownMethodException |
當呼叫未知方法時 |
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
定義於: yii\base\BaseObject::__construct()
建構子。
預設實作會執行兩件事
- 使用給定的組態
$config
初始化物件。 - 呼叫 init()。
如果子類別中覆寫了此方法,建議
- 建構子的最後一個參數是一個組態陣列,如同此處的
$config
。 - 在建構子結尾呼叫父類別實作。
public void __construct ( $config = [] ) | ||
$config | array |
將用於初始化物件屬性的名稱-值配對 |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
定義於: yii\base\BaseObject::__get()
回傳物件屬性的值。
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $value = $object->property;
時會被隱式呼叫。
另請參閱 __set()。
public mixed __get ( $name ) | ||
$name | string |
屬性名稱 |
return | mixed |
屬性值 |
---|---|---|
throws | yii\base\UnknownPropertyException |
如果屬性未定義 |
throws | yii\base\InvalidCallException |
如果屬性為唯寫 |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
定義於: yii\base\BaseObject::__isset()
檢查屬性是否已設定,亦即已定義且非 null。
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 isset($object->property)
時會被隱式呼叫。
請注意,如果屬性未定義,則將回傳 false。
public boolean __isset ( $name ) | ||
$name | string |
屬性名稱或事件名稱 |
return | boolean |
具名屬性是否已設定(非 null)。 |
---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
定義於: yii\base\BaseObject::__set()
設定物件屬性的值。
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $object->property = $value;
時會被隱式呼叫。
另請參閱 __get()。
public void __set ( $name, $value ) | ||
$name | string |
屬性名稱或事件名稱 |
$value | mixed |
屬性值 |
throws | yii\base\UnknownPropertyException |
如果屬性未定義 |
---|---|---|
throws | yii\base\InvalidCallException |
如果屬性為唯讀 |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
定義於: yii\base\BaseObject::__unset()
將物件屬性設定為 null。
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 unset($object->property)
時會被隱式呼叫。
請注意,如果屬性未定義,此方法將不執行任何操作。如果屬性為唯讀,則會拋出例外。
public void __unset ( $name ) | ||
$name | string |
屬性名稱 |
throws | yii\base\InvalidCallException |
如果屬性為唯讀。 |
---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
定義於: yii\base\BaseObject::canGetProperty()
回傳一個值,指出屬性是否可讀取。
如果符合以下條件,則屬性為可讀取:
- 類別具有與指定名稱相關聯的 getter 方法(在此情況下,屬性名稱不區分大小寫);
- 類別具有具有指定名稱的成員變數(當
$checkVars
為 true 時);
另請參閱 canSetProperty()。
public boolean canGetProperty ( $name, $checkVars = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
return | boolean |
屬性是否可讀取 |
---|
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
定義於: yii\base\BaseObject::canSetProperty()
回傳一個值,指出屬性是否可設定。
如果符合以下條件,則屬性為可寫入:
- 類別具有與指定名稱相關聯的 setter 方法(在此情況下,屬性名稱不區分大小寫);
- 類別具有具有指定名稱的成員變數(當
$checkVars
為 true 時);
另請參閱 canGetProperty()。
public boolean canSetProperty ( $name, $checkVars = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
return | boolean |
屬性是否可寫入 |
---|
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
::class
。
定義於: yii\base\BaseObject::className()
回傳此類別的完整限定名稱。
public static string className ( ) | ||
return | string |
此類別的完整限定名稱。 |
---|
public static function className()
{
return get_called_class();
}
手動指定此表格的主鍵。
public void fixPrimaryKey ( $keys ) | ||
$keys | string|array |
主鍵(可以是複合鍵) |
throws | yii\base\InvalidArgumentException |
如果指定的鍵在表格中找不到。 |
---|
public function fixPrimaryKey($keys)
{
$keys = (array) $keys;
$this->primaryKey = $keys;
foreach ($this->columns as $column) {
$column->isPrimaryKey = false;
}
foreach ($keys as $key) {
if (isset($this->columns[$key])) {
$this->columns[$key]->isPrimaryKey = true;
} else {
throw new InvalidArgumentException("Primary key '$key' cannot be found in table '{$this->name}'.");
}
}
}
取得具名的欄位元數據。
這是一個方便的方法,即使具名的欄位不存在,也能檢索它。
public yii\db\ColumnSchema|null getColumn ( $name ) | ||
$name | string |
欄位名稱 |
return | yii\db\ColumnSchema|null |
具名欄位的元數據。如果具名欄位不存在,則為 Null。 |
---|
public function getColumn($name)
{
return isset($this->columns[$name]) ? $this->columns[$name] : null;
}
回傳此表格中所有欄位的名稱。
public array getColumnNames ( ) | ||
return | array |
欄位名稱列表 |
---|
public function getColumnNames()
{
return array_keys($this->columns);
}
定義於: yii\base\BaseObject::hasMethod()
回傳一個值,指出方法是否已定義。
預設實作是對 php 函數 method_exists()
的呼叫。當您實作了 php 魔術方法 __call()
時,您可以覆寫此方法。
public boolean hasMethod ( $name ) | ||
$name | string |
方法名稱 |
return | boolean |
方法是否已定義 |
---|
public function hasMethod($name)
{
return method_exists($this, $name);
}
定義於: yii\base\BaseObject::hasProperty()
回傳一個值,指出屬性是否已定義。
如果符合以下條件,則屬性已定義:
- 類別具有與指定名稱相關聯的 getter 或 setter 方法(在此情況下,屬性名稱不區分大小寫);
- 類別具有具有指定名稱的成員變數(當
$checkVars
為 true 時);
另請參閱
public boolean hasProperty ( $name, $checkVars = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
return | boolean |
屬性是否已定義 |
---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
public void init ( ) |
public function init()
{
}
註冊 或 登入 以發表評論。