The model classes that are defined are usually placed below the Lib\model directory of the project. 1. Table Information Definition
The model class is 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. The naming convention for model classes is the name of the datasheet that removes 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:UserModel.class.php corresponds to a table named Think_user
If your rules do not conform to the above system conventions, you need to set the data table name attribute for the model class.
There is no need to add a table prefix, the table prefix is defined in the configuration file
protected $tableName = ' categories ';
The prefix of a table (top_depts) in the database is different from the other table prefixes. This property does not appear at the same time as the previous one
protected $trueTableName = ' top_depts ';
Note that Truetablename requires a complete table name definition
//optional-Defines the database
protected $dbName = ' top ';
2. Field Definition
1. Deployment mode-Automatic generation
The system automatically gets the field information for the data table (and only once for the first instantiation of the model), until the field information is permanently cached, unless the settings are not cached or deleted.
2. Debug mode-Automatic generation
The field cache file is not generated, and the data table field information is retrieved each time.
3. Manually define the advantages: You can avoid the efficiency overhead of IO loading disadvantage: After the fields attribute is defined, the data table's field information is not automatically obtained. If you have modified or added fields, you must manually modify the value of the Fields property.
Protected $fields = Array (
' id ', ' username ', ' email ', ' age ',
////_PK represents the primary key field name
' _PK ' => ' id ',
//_ Autoinc indicates whether the primary key automatically grows type
' _autoinc ' => true
);
3. Access to the field
Thinkphp supports objects and arrays two ways to access data properties
Object access
Instantiate the user model
$User = D (' user ');
Query user Data
$User->find (1);
Gets the value of the Name property
Echo $User->name;
Sets the value of the Name property
$User->name = ' thinkphp ';
$User->create ();
Echo $User->name;
Array access
Instantiate the user model
$User = D (' user ');
Query user data
$data = $User->find (1);
Gets the value of the Name property,
Echo $data [' name '];
Sets the value of the Name property
$data [' name '] = ' thinkphp ';
4. The definition of a complete model
<?php class Usermodel extends//does not require a table prefix, the table prefix is defined in the configuration file model{$tableName
= ' categories '; The prefix of a table (top_depts) in the database is different from the other table prefixes.
This property does not appear at the same time as the previous one protected $trueTableName = ' top_depts ';
Note that Truetablename requires a complete table name definition//optional-Defines the database protected $dbName = ' top '; Protected $fields = array (' ID ', ' username ', ' email ', ' age ',////_PK denotes the primary key field name ' _PK ' =
> ' id ',//_autoinc indicates whether the primary key automatically grows type ' _autoinc ' => true);
Public Function Gettopuser () {//Add your own business logic//...} }