Yii 2--model Table Prefix

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.