Heim  >  Artikel  >  Backend-Entwicklung  >  YII2中,MODEL获取表字段作为属性的方式为什么是使用“SHOW FULL COLUMNS FROM TABLE”?

YII2中,MODEL获取表字段作为属性的方式为什么是使用“SHOW FULL COLUMNS FROM TABLE”?

WBOY
WBOYOriginal
2016-08-18 09:15:332385Durchsuche

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>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn