ThinkPHP learning note instantiation model four methods

Source: Internet
Author: User

Create Action class [php] <? Php class NewObjectAction extends Action {public function index () {// 1. create a basic Model // 1: instantiate the database operation class of a system // new Model ('user') is equivalent to M ('user '), put a table to be operated in the Model /// 1 Name: //// thinkphp Default table name prefix, if it is think_user; you can use the User or user in the Model to obtain the // if it is think_user_message; then use Model ('usermessage '); in upper case, A _ // $ User = new Model ('user') is added before the table name '); //// call the query method // $ list = $ User-> select (); // dump ($ list); //// 2: Cross-model operations; instantiate a table and Instantiate a self-written database operation class // mostly used to extract some public business logic to form a public Model // M ('user', 'commonmodel ') equivalent to new CommonModel ('user'); model is automatically loaded // $ user = M ('user', 'commonmodel '); // $ user = new CommonModel ('user'); // $ list = $ user-> select (); // dump ($ list ); // $ user-> modelTest (); // 3: instantiate a user-defined model. // multiple models are used for businesses that do not need to be more complex. // 1. create a model manually and create a custom model for the user table. The function provided by thinkphp can be encapsulated. // $ user = new UserModel () is equivalent to D ('user '); // method D if the model does not exist, an exception is thrown and only one instance is used. By default, only calls are supported. Model under the current application // $ user = new UserModel (); // $ list = $ user-> select (); // dump ($ list ); // $ user-> modelTest (); // 4. instantiate an empty Model. It is a traditional SQL statement. You need to add the table prefix // $ user = new Model (); $ list = $ user-> query ('select * from tb_user '); dump ($ list); // group: D ('admin. user ') ;}}?> Two models: [php] <? Php class CommonModel extends Model {function modelTest () {echo 'test cross-Model operations, call the method in the Model ';}}?> [Php] <? Php class UserModel extends Model {function modelTest () {echo 'test cross-Model operations, call the method in the Model ';}}?>

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.