ThinkPHP instantiation model

Source: Internet
Author: User
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:

  1. Class CommonModel extends Model {
  2. Function abc (){
  3. Echo "the methods abc output in my common model ";
  4. }
  5. }

Under the Action folder:

  1. $ User = M ('user', 'commonmodel'); <=>$ User = new CommonModel ('user ');
  2. $ List = $ user-> select ();
  3. Dump ($ list );
  4. $ 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:

  1. Class UserModel extends Model {
  2. Function aa (){
  3. Echo "aaaaaaaaaaaaaaaaa ";
  4. }
  5. }

Under the Action folder:

  1. $ User = new UserModel (); <=>$ user = D ("User ");
  2. $ List = $ user-> select ();
  3. Dump ($ list );
  4. $ 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:

  1. $ User = new Model (); <=>$ user = M ();
  2. $ List = $ user-> query ("select * from think_user ");
  3. 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 ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.