ThinkPHP instantiation model

Source: Internet
Author: User
1. instantiate a table. 1. create a basic model. instantiate a database class that comes with the system. UsernewModol (user) is equivalent to $ UserM (user ); 2. thinkphp will automatically help us find the table in this database, and take the table as the corresponding field.

I. instantiate a table

1. create a basic model, instantiate a database class that comes with the system & User = new Modol ('user') is equivalent to $ user = M ('user ');

2. thinkphp will automatically help us find the table in this database and take all the corresponding fields in the table, you can directly operate the table and use the methods in the table (add, delete, modify, and query) $ list = $ User-> select ()

II. instantiate a self-written custom database class and a table

  1. $ USER = M ('user', 'commonmodel'); equivalent to $ USER = new commonmodel ('user') // COMMONMODEL
  2. $ LIST = $ USER-> SELECT ();
  3. DUMP ($ LIST)
  4. $ USER-> ABD (); // methods in the COMMONMODEL class

3. instantiate a user-defined model

1. Custom model: // home/Lib/Action/

  1. Class UserModel extends Model {
  2. Function ad (){
  3. Echo 'aaaaaaaaaaa ';
  4. }
  5. }

2. instantiate the model:

Note:When instantiating a model, the table name and your class name (model) do not need to be input. in this case, the relevant fields of the table are obtained.

  1. $ User = new UserModel (); equivalent to $ user = D ('user ');
  2. $ List = $ user-> select (); // select () is inherited from Moel.
  3. Dump ($ list)
  4. $ User-> ad ();

4. strengthen an empty model:

  1. $ User = new Model ();
  2. $ List = $ user-> query ('select * from think_user '); // you must add a prefix to the table.
  3. Dump ($ list );

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.