Home > Backend Development > PHP Tutorial > 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
Release: 2016-08-18 09:15:33
Original
2450 people have browsed it

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>
Copy after login
Copy after login
<code>    protected function findColumns($table)
    {
        $sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteTableName($table->fullName);
        try {
            $columns = $this->db->createCommand($sql)->queryAll();
        } 
        .........
    }</code>
Copy after login
Copy after login

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>
Copy after login
Copy after login
<code>    protected function findColumns($table)
    {
        $sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteTableName($table->fullName);
        try {
            $columns = $this->db->createCommand($sql)->queryAll();
        } 
        .........
    }</code>
Copy after login
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template