Method 1: Create a basic model (instantiate a database operation class that comes with the system); 2.0 later, it is more scientific and convenient. When instantiating a database operation class, you do not need to define a database operation class by yourself, thinkphp will automatically find the database.
Four ways to instantiate a model:
Method 1:Create a basic model (instantiate a database operation class that comes with the system); 2.0 later, it is more scientific and convenient. When instantiating a database operation class, you do not need to define a database operation class by yourself, thinkphp will automatically find the table in the database and take all the fields in the table. the directly operated object is the operated table:
$ User = new Model ("User"); <=>$ user = M ("User ");
The first letter of the User is in upper case. when we name the table, the table name is think_user. in this case, thinkphp will tell you whether you have an underline before, think_user_message_id, $ mes = new Model ("UserMessageID"); write think_userMessageId.
Method 2:When instantiating a table, you can also instantiate your own custom database model operation class.
Under the Model folder:
- Class CommonModel extends Model {
- Function abc (){
- Echo "the methods abc output in my common model ";
- }
- }
Under the Action folder:
- $ User = M ('user', 'commonmodel'); <=>$ User = new CommonModel ('user ');
- $ List = $ user-> select ();
- Dump ($ list );
- $ List-> abc ();
Method 2: implement more complex business logic
Method 3: (instantiate a user-defined model)
You need to manually create a model. you can write a custom model for this table (such as user). In this model, you can encapsulate the advanced functions provided by thinkphp, some custom methods can be encapsulated to implement more complex business logic.
Under the Model folder:
- Class UserModel extends Model {
- Function aa (){
- Echo "aaaaaaaaaaaaaaaaa ";
- }
- }
Under the Action folder:
- $ User = new UserModel (); <=>$ user = D ("User ");
- $ List = $ user-> select ();
- Dump ($ list );
- $ User-> aa ();
The second and third modes are different.
Method 2: In your business logic, there are usually some public business logic. it is very difficult to define a model separately each time, public business logic can be placed in CommonModel.
Method 3: applicable to more complex business logic of the table to be operated, but it does not need to use public business logic (its business logic
It is unique for user tables and does not need to be used in other models)
Method 4:This method instantiates an empty model and does not know which table you want to instantiate. at this time, the query statement must add the table prefix:
- $ User = new Model (); <=>$ user = M ();
- $ List = $ user-> query ("select * from think_user ");
- Dump ($ list );
Another: The D () method is different from the new Model () method:
D () method:Automatically detects model classes. if a model class does not exist, an exception is thrown and an instantiated model is not repeated.
For instantiation, the default D () can only call models under the current project (application). For foreground projects (home), How to instantiate models of background projects (admin: $ user = D ('admin', 'User'); if grouping is enabled: $ User = D ("admin. user ");