Home  >  Article  >  Backend Development  >  php framework - thinkPHP M model table prefix: When to use $tablePrefix and when to use 'DB_PREFIX'?

php framework - thinkPHP M model table prefix: When to use $tablePrefix and when to use 'DB_PREFIX'?

WBOY
WBOYOriginal
2016-12-01 01:27:172238browse

I know that when creating an M model, there are two ways to define the table prefix,
One is in $tablePrefix and the other is in $connection,

Why do I use

here?
<code>protected $connection = array
    (
        'DB_TYPE' => 'mysql',    //数据库类型
        'DB_USER' => 'root',    //用户名
        'DB_PWD'  => '',    //密码
        'DB_HOST' => 'localhost',    //域名
        'DB_PORT' => '3306',    //??
        'DB_NAME' => 'minute1',    //库名
        'DB_CHARSET' => 'UTF8',    //编码
        'DB_PREFIX' => 'minute1_'    //★★★★★★★★★★★★数据库表前缀minute1_
    );</code>

No, it’s better to use protected $tablePrefix="minute1_". When should I use which one?

Reply content:

I know that when creating an M model, there are two ways to define the table prefix,
One is in $tablePrefix and the other is in $connection,

Why do I use

here?
<code>protected $connection = array
    (
        'DB_TYPE' => 'mysql',    //数据库类型
        'DB_USER' => 'root',    //用户名
        'DB_PWD'  => '',    //密码
        'DB_HOST' => 'localhost',    //域名
        'DB_PORT' => '3306',    //??
        'DB_NAME' => 'minute1',    //库名
        'DB_CHARSET' => 'UTF8',    //编码
        'DB_PREFIX' => 'minute1_'    //★★★★★★★★★★★★数据库表前缀minute1_
    );</code>

No, it’s better to use protected $tablePrefix="minute1_". When should I use which one?

$tablePrefix is ​​defined in the Model, and its priority is higher than that in the configuration file. Generally speaking, the initial table prefixes in your project are all "a_", which you defined in the configuration file

<code>'DB_PREFIX'=>'a_'</code>

But a b_temp table is added later. If it is not processed when instantiating temp, the system will look for a_temp. This table does not exist. At this time, you need to define $tablePrefix='b_' in the Temp model. ;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn