首頁  >  文章  >  後端開發  >  YII2中,MODEL取得表格欄位作為屬性的方式為什麼是使用「SHOW FULL COLUMNS FROM TABLE」?

YII2中,MODEL取得表格欄位作為屬性的方式為什麼是使用「SHOW FULL COLUMNS FROM TABLE」?

WBOY
WBOY原創
2016-08-18 09:15:332386瀏覽

YII2中,MODEL取得表格欄位作為屬性的方式是使用“SHOW FULL COLUMNS FROM TABLE”,而不是直接寫在MODEL中。這樣不時讀資料庫信息,這種額外開銷感覺沒必要,畢竟表結果不常修改的。是基於什麼原因使用這種方式呢?

<code>    protected function loadTableSchema($name)
    {
        $table = new TableSchema;
        $this->resolveTableNames($table, $name);

        if ($this->findColumns($table)) {
            $this->findConstraints($table);

            return $table;
        } else {
            return null;
        }
    }</code>
<code>    protected function findColumns($table)
    {
        $sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteTableName($table->fullName);
        try {
            $columns = $this->db->createCommand($sql)->queryAll();
        } 
        .........
    }</code>

回覆內容:

YII2中,MODEL取得表格欄位作為屬性的方式是使用“SHOW FULL COLUMNS FROM TABLE”,而不是直接寫在MODEL中。這樣不時讀資料庫信息,這種額外開銷感覺沒必要,畢竟表結果不常修改的。是基於什麼原因使用這種方式呢?

<code>    protected function loadTableSchema($name)
    {
        $table = new TableSchema;
        $this->resolveTableNames($table, $name);

        if ($this->findColumns($table)) {
            $this->findConstraints($table);

            return $table;
        } else {
            return null;
        }
    }</code>
<code>    protected function findColumns($table)
    {
        $sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteTableName($table->fullName);
        try {
            $columns = $this->db->createCommand($sql)->queryAll();
        } 
        .........
    }</code>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn