Detailed Explanation of table Method of ThinkPHP CURD Method

  • 2021-07-01 06:54:01
  • OfStack

The table method of the ThinkPHP CURD method also belongs to one of the coherent operation methods of the model class, which is mainly used to specify the data table of the operation.

The specific usage is as follows:

1 Under normal circumstances, the system can automatically identify the current corresponding data table when operating the model. Therefore, the table method is usually used to:

1. Switch the data table of the operation;
2. Operate on multiple tables;

For example:


$Model->table('think_user')->where('status>1')->select();

You can also specify a database in the table method, for example:


$Model->table('db_name.think_user')->where('status>1')->select();

It should be noted that the table method does not change the database connection, so you should make sure that the currently connected user has permission to operate the corresponding database and data table.

After switching the data table, the system will automatically retrieve the field cache information of the switched data table.

If you need to operate on multiple tables, you can use:


$Model->field('user.name,role.title')->table('think_user user,think_role role')->limit(10)->select();

In order to avoid keyword conflicts with mysql as much as possible, it is recommended to use array definition, such as:


$Model->field('user.name,role.title')->table(array('think_user'=>'user','think_role'=>'role'))->limit(10)->select();


Related articles: