Overview of four methods of thinkphp instantiation model _php Instance

Source: Internet
Author: User

This paper describes four methods of thinkphp instantiation model, which is very important for thinkphp programming. Specifically as follows:

1. Create an underlying model: instantiate a system with its own database operations class

The Test.Model.class.php page code is as follows:

  Class Testmodel extends model{
   
  }

The UserAction.class.php page code is as follows:

  function test () {
    $test =m (' test ');//indicates that the model class is instantiated, and that the test value is passed in to the test table
    //equivalent to $test=new Testmodel ();
    $test = $test->select ();
    Print_r ($test);//output all data in test table
  

2. Instantiate a custom model

If the database operation is more complex, you need to add some custom database manipulation methods in the custom model class

The UserModel.class.php page code is as follows:

  Class Usermodel extends model{
    function Pyj () {
      echo ' Pengyanjie ';
      Some of the other database operations methods
    }
  }

The UserAction.class.php page code is as follows:

  function User () {
    $user =d (' user ');//Instantiate the custom database operation class
    //equivalent to $user=new Usermodel ();
    $user->pyj ()//Call Pyj method in the user model
  } 

Or, you need to instantiate a table and, at the same time, instantiate a custom database action class that you write yourself, with the following code:

  function Love () {
    $love =m (' Test ', ' Usermodel ');  
    $love =new usermodel (' test '); 
    $list = $love->select ();
    Dump ($list);
    $love->pyj ();
  } 

3, the instantiation of a user model

The UserAction.class.php page code is as follows:

  function User () {
    $user =new usermodel ();//equal to $user=d (' user ');
    $list = $user->select ();
    Dump ($list);
    echo $user->aa ();
  }

The UserModel.class.php page code is as follows:

The class name user corresponds to the table name user, so when instantiating the model in Useraction, no additional table names are required, as follows:

  Class Usermodel extends model{
    function aa () {
      echo ' Pengyanjie ';
    }
  }

The third type of instantiation model differs from the second: in your business logic, there will usually be some common business logic, then you can use the second m (' table name ', ' model name '), such as M (' user ', ' Commonmodel ') to be more convenient;

The third instantiation model approach is suitable for more complex business logic for the table being manipulated, but it does not require the use of public business logic. (Its business logic, for user tables, is unique and does not need to be used in other models).

4. Instantiate an empty model, and it doesn't know which table to use when you instantiate the operation.

  $user =new Model ();//Equivalence and $user=m ();
  $list = $user->query (' select * from Think_user '); Using a traditional SQL statement, if so, you must add a table prefix 
  dump ($list);

Attached: $user =new Usermodel (); the difference with $user=d (' user '):

(1), D method can automatically detect the model class, when it does not exist, it throws an exception. At the same time, the instantiated model is not repeated. The default D method, which can only be applied to the model below the current project.

(2), if you say, I this is the foreground application, but I want to instantiate the background project model can be used D to fix.

$user =d (' admin ', ' user ');//will automatically find the user model class under the Admin group

Or:

$user =d (' Admin.user ');

It is hoped that the examples described in this paper will help us thinkphp program design.

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.