Home  >  Article  >  PHP Framework  >  Can thinkphp distinguish different data tables based on prefixes?

Can thinkphp distinguish different data tables based on prefixes?

PHPz
PHPzOriginal
2023-04-13 18:31:46748browse

ThinkPHP is a very excellent PHP development framework. It has completely different needs for WEB development. Considering the modular development idea, ThinkPHP provides one practical function after another. Among them, the different module table prefix functions are worth checking out. Mention is also one of the features that developers who deploy multiple systems in the same database cannot miss.

1. Why deploy multiple systems in the same database

In actual WEB development, you will usually encounter situations where you need to integrate multiple systems in the same Web application. Depending on the situation, these systems may involve different areas of the business. If all systems are integrated into one application, it will not only be difficult to develop, but also extremely easy to cause code confusion, and may also lead to VIP deadlock. As a result, in order to ensure convenient and efficient development, we need to deploy multiple systems in the same database.

2.ThinkPHP modular development

ThinkPHP adopts MVC architecture design. In order to better solve our above problems, we can adopt modular development ideas. The so-called modularization is to split a large application into several independent modules. During the module design process, we can equip different modules with different database table prefixes to avoid conflicts between table names.

3.ThinkPHP modular functional features

In the process of modular development, ThinkPHP provides a series of applications such as view layer, control layer, and model layer. In particular, the control layer function can Let multiple modules interact with each other, call each other, complete operations efficiently, and build our final application. The control layer is an important part connected between the business logic layer and the view layer in the MVC architecture. It can be said to be like a fish in water. Of course, above the above levels, there are also two practical functions of ThinkPHP routing and caching specially designed for code optimization.

4.ThinkPHP table prefix settings for different modules

For table prefix settings for different modules in the same database, we only need to add 'MODULE prefix'_TABLE_PREFIX to the corresponding configuration file in the module Global array variable and add it to the array configured in the database.

$reslist=M('Subdirectory name/Test')->select();

The M('') function is the module name in the model, and the second part of M The first parameter is the name of the controller in the module. The select() method is used to query data, and the parameters do not need to be passed. 2. Solution

The example code is as follows:

$config=array (

'DB_TYPE'=>'mysql',   
'DB_HOST'=>'localhost', 
'DB_USER'=>'root',  
'DB_PWD'=>'', 
'DB_PORT'=>3306, 
'DB_NAME'=>'test2333',
'DB_PREFIX'=>'nov_',        
'MODULE_ALLOW_LIST'    => array('Home', 'Admin'),
'DEFAULT_MODULE'       => 'Home',
'SESSION_PREFIX' => 'api_',
'LOG_RECORD'=>false,
'LOG_TYPE'=> 'Db',
'LOAD_EXT_CONFIG' => 'extconfig',

);

If you need to configure a module named "Test" and set the table prefix for the module, we can add the following global array in the configuration file Variable:

'MODULE_TEST_TABLE_PREFIX' => 'tb_'

In this way, when using "$this->M('Test/TBid')->select(); "In such code calls, ThinkPHP can distinguish different database tables based on prefixes.

The above is the detailed content of Can thinkphp distinguish different data tables based on prefixes?. For more information, please follow other related articles on the PHP Chinese website!

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