Home > Backend Development > PHP Tutorial > Overview of the four methods of instantiating models in ThinkPHP, four methods of thinkphp_PHP tutorial

Overview of the four methods of instantiating models in ThinkPHP, four methods of thinkphp_PHP tutorial

WBOY
Release: 2016-07-13 10:20:09
Original
933 people have browsed it

Overview of the four methods of instantiating models in ThinkPHP, four methods of thinkphp

This article describes four methods of instantiating models in ThinkPHP, which has very important applications in ThinkPHP programming. The details are as follows:

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

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

  class TestModel extends Model{
   
  }

Copy after login

UserAction.class.php page code is as follows:

  function test(){
    $test=M('test');//表示实例化的是自带的Model类,并且传入test值表示操作的是test表
    //等同于$test=new TestModel();
    $test=$test->select();
    print_r($test);//输出test表中所有数据
  } 
Copy after login

2. Instantiate a custom model

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

UserModel.class.php page code is as follows:

  class UserModel extends Model{
    function pyj(){
      echo 'pengyanjie';
      //其它的一些数据库操作方法
    }
  }

Copy after login

UserAction.class.php page code is as follows:

  function user(){
    $user=D('User');//实例化自定义的数据库操作类
    //等同于$user=new UserModel();
    $user->pyj();//调用User模型中的pyj方法
  } 

Copy after login

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

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

3. Instantiate a user model

UserAction.class.php page code is as follows:

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

Copy after login

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

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

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

Copy after login

The difference between this third instantiation model method and the second one is: in your business logic, usually there will be some public business logic, then you use the second M('table name', 'Model name'); such as M('user','CommonModel') will be more convenient;

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

4. Instantiate an empty model. It does not know which table you want to use when instantiating the operation.

  $user=new Model();//等价与$user=M();
  $list=$user->query('select * from think_user'); //使用传统的sql语句的方式,如果这样的话,就必须要加表前缀 
  dump($list);

Copy after login

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

(1). The D method can automatically detect the model class. If it does not exist, it will throw an exception. At the same time, models that have been instantiated will not be instantiated repeatedly. The default D method can only be applied to models under the current project.

(2) If I say this is a front-end application, but I want to instantiate the model of the back-end project, I can use D.

$user=D('admin','user');//会去自动找admin分组下的user模型类
Copy after login

or:

$user=D('admin.user');
Copy after login

I hope the examples described in this article will be helpful to everyone in ThinkPHP programming.

thinkphp instantiation model problem

Add your database connection information to the configuration file you generated separately.
It should be added to the config.php file in the Conf directory with your database connection information.

I am new to thinkphp. After I instantiated the model class, I got an error. Here is my code. Please help me take a look.

'URL_MODEL' =>1
Is there a comma missing after 1 in English state?
Remember to delete the runtime folder.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/868236.htmlTechArticleOverview of the four methods of ThinkPHP instantiation model, thinkphp four methods This article describes the four methods of ThinkPHP instantiation model Method has a very important application in ThinkPHP programming. ...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template