1.1 ModelTable Prefixes
In most systems, the table name for the database likes to add a prefix, such as the table name of the Ecshop system, with the prefix "Ecs_", which is filled with thefollowing code in their code:
$GLOBALS [' ECS ']->table (' goods ')
The purpose of this code is to put the prefix "ECS" and the following name together to obtain "ecs_goods" such as the actual table name. Why not just stitch the string "Ecs_" and "goods"? Because the table prefix can be configured at the time of installation, it is not possible to pin the string concatenation of the write dead strings.
in the Yii system, the database table is the model corresponding to the table name is returned in the model class tableName () function, in order to deal with the table prefix problem, in this function to return the table name, You can use code similar to the following:
Public static function TableName () { return ' {{%user}} '; } |
As you can see, the return User table name is not returned directly to users , but {{%user}}, replacing the% with the table prefixwhen actually extracting data from the database .
How do I configure the application's table prefixes? Configure in the db parameter of the main.php:
return[ ' components '= [ ' DB '= [ ' class '=' Yii\db\connection ', ' DSN '=' Mysql:host=localhost;dbname=liduoo ', ' username '=' Root ', ' Password '=' MySQL ', ' CharSet '=' UTF8 ', ' tableprefix ' = ' abc_ ', ], ], ]; |
This article is from the "Rainman" blog, make sure to keep this source http://lancelot.blog.51cto.com/393579/1871258
Yii 2--model Table Prefix