Home  >  Article  >  Backend Development  >  In YII2, why does MODEL use "SHOW FULL COLUMNS FROM TABLE" to obtain table fields as attributes?

In YII2, why does MODEL use "SHOW FULL COLUMNS FROM TABLE" to obtain table fields as attributes?

WBOY
WBOYOriginal
2016-08-18 09:15:332386browse

In YII2, the way for MODEL to obtain table fields as attributes is to use "SHOW FULL COLUMNS FROM TABLE" instead of writing them directly in MODEL. This extra overhead of reading database information from time to time seems unnecessary, after all, the table results are not modified frequently. What is the reason for using this method?

<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>

Reply content:

In YII2, the way for MODEL to obtain table fields as attributes is to use "SHOW FULL COLUMNS FROM TABLE" instead of writing them directly in MODEL. This extra overhead of reading database information from time to time seems unnecessary, after all, the table results are not modified frequently. What is the reason for using this method?

<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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn