php - YII2 MODEL获取表字段方式
怪我咯
怪我咯 2017-04-11 10:24:49
0
2
634

YII2中,MODEL获取表字段作为属性的方式是使用“SHOW FULL COLUMNS FROM TABLE”,而不是直接写在MODEL中。这样不时读数据库信息,这种额外开销感觉没必要,毕竟表结果不经常修改的。是基于什么原因使用这种方式呢?

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

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

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

走同样的路,发现不同的人生

全部回覆(2)
伊谢尔伦

你可以在生产环境中使用字段缓存,配置 db.php

'enableSchemaCache' => true,
伊谢尔伦
    'enableSchemaCache' => true,
    // Name of the cache component used to store schema information
    'schemaCache' => 'cache',
    // Duration of schema cache.
    'schemaCacheDuration' => 86400, // 24H it is in seconds
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!