Yii2 Gii Table Prefix

吃可爱长大的小学妹 提交于 2019-12-07 04:18:56

问题


I allways setup table prefix - for this post lets say my prefix is abc_. So in common\config\main-local.php. I have:

'components' => [
'db' => [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=database',
    'username' => 'user',
    'password' => 'pwd',
    'charset' => 'utf8',
    'tablePrefix' => 'abc_',
],
...

I have worked on Yii1 and used gii to generate models. In this version it generated files like: table.php.

Now I work with Yii2 and learn the differences: gii generate files like abc_table.php. Yes - I checked "Use Table Prefix".

This is not ok because prefix should be transparent. Could please anyone tell me what I'm doing wrong?


回答1:


You may change model class name AbcTest to Test. And check Use Table Prefix. Gii generate correct model like that:

class Test extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%test}}';
    }
    ...
}

In tableName method return '{{%test}}' if you check Use Table Prefix. If you not check Use Table Prefix in this method return 'abc_test'.



来源:https://stackoverflow.com/questions/28998385/yii2-gii-table-prefix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!