ThinkPHP instantiation model overview, thinkphp four methods

Source: Internet
Author: User

ThinkPHP instantiation model overview, thinkphp four methods

This article 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 UserAction. class. php Page code is as follows:

Function test () {$ test = M ('test'); // It indicates that the built-in Model class is instantiated, the input value of test indicates that the test table is operated // equivalent to $ test = new TestModel (); $ test = $ test-> select (); print_r ($ test ); // output all data in the test table}
 

2. instantiate a custom Model

If database operations are complex, you need to add some custom database operation methods to the custom Model class.

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

Class UserModel extends Model {function pyj () {echo 'pengyanjie '; // other database operation methods }}

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

Function user () {$ user = D ('user'); // instantiate a custom database operation class // equivalent to $ User = new UserModel (); $ user-> pyj (); // call the pyj method in the User model}

Alternatively, you need to instantiate a table, and at the same time, instantiate a self-written custom database operation class. The Code is as follows:

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

3. instantiate a user model

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

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

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

This class name user corresponds to the table name user, so you do not need to upload the table name when instantiating this model in UserAction. The Code is as follows:

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

The third instantiation model method differs from the second one: In your business logic, there are usually some public business logic, then you can use the second M ('table name', 'model name'). For example, M ('user', 'commonmodel') is more convenient;

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

4. instantiate an empty model. It does not know which table is used for instantiation.

$ User = new Model (); // equivalent to $ user = M (); $ list = $ user-> query ('select * from think_user '); // use traditional SQL statements. In this case, you must add the table prefix dump ($ list );

Appendix: $ user = new UserModel (); and $ user = D ('user:

(1) method D can automatically detect the model class. If it does not exist, it throws an exception. At the same time, models that have already 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'); // The user model class in the admin group is automatically found.

Or:

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

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


Thinkphp instantiation Model

Add the information about your database connection to the configuration file generated by yourself.
It should be the config. php file in the Conf directory that contains the information about your database connection.
 
When I first came into contact with thinkphp, I reported an error after instantiating the model class. below is my code. Please take a look at it for me.

'Url _ model' => 1
Is there a comma in the English state after "1.
Remember to delete the runtime folder.

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.