The table method of the ThinkPHP CURD method is also one of the coherent operation methods of the model class. This method is mainly used to specify the data table for the operation.
The specific usage is as follows:
Generally, the system can automatically identify the current corresponding data table when operating the model, so the table method is usually used for:
1. Data table of switching operation;
2. Operate multiple tables;
For example:
$Model->table('think_user')->where('status>1')->select();
You can also specify the 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 will not change the database connection, so you must ensure that the currently connected user has permission to operate the corresponding database and data table.
After switching data tables, the system will automatically re-obtain the field cache information of the switched data table.
If you need to operate multiple tables, you can use it like this:
$Model->field('user.name,role.title')->table('think_user user,think_role role')->limit(10)->select();
In order to avoid conflict with mysql keywords, it is recommended to use array definition, for example:
$Model->field('user.name,role.title')->table(array('think_user'=>'user','think_role'=>'role'))->limit(10)->select();