Model classes are generally located under the Lib/model directory of the project, and when we create a Usermodel class, we have actually followed the system's conventions. A naming convention for a model class is a data table name that drops the table prefix, is named with the Hump method, and the first letter is capitalized, and then the model class suffix is defined, for example:
model name (class name) |
contract corresponding data table (assuming the database prefix definition is THINK_) |
Usermodel |
Think_user |
Usertypemodel |
Think_user_type |
If your rule does not match the above system conventions, you need to set the data table Name property of the Model class.
In the thinkphp model, there are several attribute definitions about the name of the data table:
Properties |
Description |
TableName |
A data table name that does not contain a table prefix, typically the default is the same as the model name, which needs to be defined only if your table name differs from the name of the current model class. |
Truetablename |
The name of the data table that contains the prefix, which is the actual table name in the database, which does not need to be set, only if the above rules do not apply, or in special cases. |
DbName |
Define the database name that the model currently corresponds to, only if you have a different database name and configuration file for your current model class. |
Here is an example to deepen understanding, for example, in the database there is a think_categories table, and we define the Model class name is Categorymodel, according to the system, the name of the model is category, the corresponding data table name should be THINK_ Category (all lowercase), but now the data table name is think_categories, so we need to set the TableName property to change the default rule (assuming we have defined db_prefix as think_ in the config file).
- protected $tableName = ' categories ';
Note that the definition of this property does not require a table prefix THINK_
And for another special case, the database has a table (top_depts) prefix and other table prefixes are different, not think_ but top_, this time we need to define the Truetablename property
- protected $trueTableName = ' top_depts ';
Note Truetablename requires a full table name definition
In addition to the definition of a data table, you can define a database, for example:
Li class= "L0" style= "padding:0px 5px; margin:0px; border-left-width:1px; Border-left-style:solid; Border-left-color:rgb (209,215,220); line-height:23px; Background-color:rgb (250,250,250) "> protected $dbName =   ' top '
4
thinkphp Model Definition