Yii2 Gii Table Prefix
Asked Answered
R

1

5

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?

Razo answered 11/3, 2015 at 22:12 Comment(0)
P
7

You may change the model class name AbcTest to Test. For future model generations, check the Use Table Prefix field in the Gii tool. Gii generate correct model like this:

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

In tableName method, it returns '{{%test}}' if you check Use Table Prefix in the Gii tool. If you do not check the Use Table Prefix, this method return 'abc_test' and generated model class will be named as AbcTest.

Penetralia answered 12/3, 2015 at 7:39 Comment(1)
Thanks - I should have seen this - I always referred to suggested valuesRazo

© 2022 - 2024 — McMap. All rights reserved.