Overview of four methods of ThinkPHP instantiation model

  • 2021-07-13 04:56:49
  • OfStack

This paper describes four methods of ThinkPHP instantiation model, which are very important for ThinkPHP programming. The details are as follows:

1. Create a basic model: instantiate a database operation class that comes with the system

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


  class TestModel extends Model{
   
  }

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


  function test(){
    $test=M('test');// Indicates that the instantiated is self-contained Model Class, and pass in the test Value represents the operation is test Table 
    // Equivalent to $test=new TestModel();
    $test=$test->select();
    print_r($test);// Output test All data in the table 
  } 

2. Instantiate a custom model

If the database operation is complicated, you need to add some custom database operation methods in the custom Model class

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


  class UserModel extends Model{
    function pyj(){
      echo 'pengyanjie';
      // Others 1 Some database operation methods 
    }
  }

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


  function user(){
    $user=D('User');// Instantiate a custom database action class 
    // Equivalent to $user=new UserModel();
    $user->pyj();// Call User In the model pyj Method 
  } 

Or, you need to instantiate a table, and at the same time, instantiate a custom database operation class written by yourself, with the following code:


  function love(){
    $love=M('test','UserModel');  
    //$love=new UserModel('test'); 
    $list=$love->select();
    dump($list);
    $love->pyj();
  } 

3. Instantiate a user model

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


  function user(){
    $user=new UserModel();// Equivalent to $user=D('User');
    $list=$user->select();
    dump($list);
    echo $user->aa();
  }

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

The class name user corresponds to the table name user, so there is no need to pass an additional table name when instantiating the model in UserAction. The code is as follows:


  class UserModel extends Model{
    function aa(){
      echo 'pengyanjie';
    }
  }

The difference between this third instantiation model method and the second is that in your business logic, there will usually be some common business logic, so you use the second M ('Table Name', 'Model Name'); For example, M ('user', 'CommonModel') will be more convenient;

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

4. Instantiate an empty model, which does not know which table you will use when instantiating the operation.


  $user=new Model();// Equivalent to $user=M();
  $list=$user->query('select * from think_user'); // Use the traditional sql Statement, if so, you must prefix the table  
  dump($list);

Attachment: $user=new UserModel (); And $user = D ('user'); The difference:

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

(2) If I say that this is a foreground application, but I want to instantiate the model of the background project can be done with D.


$user=D('admin','user');// Will find it automatically admin Under grouping user Model class 

Or:


$user=D('admin.user');

I hope the examples described in this paper are helpful to everyone's ThinkPHP programming.


Related articles: