Home > Backend Development > PHP Tutorial > 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
Release: 2023-03-02 09:46:02
Original
2260 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

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. ;

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template