Model of thinkphp framework (database query)-haohaizi 1. model definition
The file name must be a table name + Model. class. php, for example, UserModel. class. php.
Namespace Home \ Model; // namespace of the Model class use Think \ Model; // The following Model class must inherit the Model base class, and the Model class is a Model class defined by the system, in the namespace Think \ Model, class UserModel extends Model {}
The class name in the file is consistent with the "table name" in the file name (this item is required. if it is inconsistent, the model class will not be found in the controller ).
The ing between class name and table name is (note: The model name corresponds to the first of each data table in uppercase with an underline, as shown in the second row below)
Model Name |
Conventions for data tables (if the database prefix is think _) |
UserModel |
Think_user |
UserTypeModel |
Think_user_type |
2. model instantiation
2.1 direct instantiation
2.1.1 $ user = new \ Home \ Model \ UserModel () without parameter instantiation ();
2.1.2 Parameter instantiation $ user = new \ Home \ Model \ UserModel (['model name'], ['data table prefix'], ['database connection information']);
The difference between the two methods is that the first method reads back the default configuration during instantiation (for example, the configuration in the model or the database configuration in the module ), the second method is to directly read the database configuration of input parameters.
The Model name is the name of the table whose prefix is removed from the database. If this parameter is not specified, the name of the Model class will be read but the name after the Model is removed, for example:
UsereModel. class. usereModel class in the php file. if the model name "User" is input during instantiation, the prefix + "user" of the input query data table will be followed ", if this parameter is not specified, the UsereModel class "Usere" is extracted to search for the data table. This solves the inconsistency between the model name and data.
2.2 D method instantiation
D. the parameter of the method is the model name and is consistent with the upper-case definition of the model class.
Cross-module instantiation
D ('admin/user') where "Admin" is the module name and "User" is the model name.
Instantiate the Info model under the Extend extension space
D ('extend: // Editor/info ')
2.3 M method instantiation
The difference between M ('user') and D method instantiation is that this method does not require the model to exist, and if the model exists, the methods in this method should not be ignored, this method is mainly used for database operations.
When the D method does not find the model, the M method is called by default.
2.4 instantiate an empty model
$ Model = new Model () or $ Model = M ();
This blank instance can be used for native SQL queries (Note: Other models can also perform native SQL queries, and the queried table does not need to correspond to the model)
3. Fields in the model
$ Fields = array ('','') indicates fields in the data table.
$ Pk indicates the primary key of the data table.
$ Connection = array ('Db _ type' => 'mysql', 'DB _ user' => 'root', 'DB _ pwd' => '123 ','Db _ host' => 'localhost ','Db _ port' => '123 ','Db _ name' => 'thinkphp ','Db _ charset' => 'utf8 ','Db _ params '=> array (), // not required); Indicates database connection
Database type: // user name: password @ database address: database port/database name # Character set
$ Connection = 'MySQL: // root: 1234 @ localhost: 3306/thinkphp # utf8 ';