ThinkPHP four methods for learning the note instantiation model _ PHP Tutorial

Source: Internet
Author: User
ThinkPHP provides four methods for learning the note instantiation model. Create Action Class [php]? PhpclassNewObjectActionextendsAction {publicfunctionindex () {1. create a basic model 1: instantiate a system database operation class new Create Action Class

[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'), and a table to be operated is placed in the Model.

/// 1 name:

/// Thinkphp has the table name prefix by default. if it is think_user, you can use User or user in the Model to obtain the table name prefix.

/// If think_user_message is used, Model ('usermessage') is used. in uppercase, _

// $ User = new Model ('user ');

/// Call the query method

// $ List = $ User-> select ();

// Dump ($ list );

/// 2: Cross-model operations. instantiate a table and a self-written database operation class.

// Mostly used to extract some public business logic to form a public Model

/// M ('user', 'commonmodel') is 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

// Mostly used for businesses that do not require more complex services

// 1. manually create a model and create a custom model for the user table, which can encapsulate the functions provided by thinkphp

// $ 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 models under the current application can be called.

// $ 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 a table prefix.

//

$ User = new Model ();

$ List = $ user-> query ('select * from tb_user ');

Dump ($ list );

// Group: D ('admin. user ');

}

}

?>

Two models:

[Php]

Class CommonModel extends Model {

Function modelTest (){

Echo 'test cross-model operations and call methods in the model ';

}

}

?>

[Php]

Class UserModel extends Model {

Function modelTest (){

Echo 'test cross-model operations and call methods in the model ';

}

}

?>

Why [php]? Php class NewObjectAction extends Action {public function index () {// 1. create a basic model // 1: instantiate the database operation class of a system /// new...

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.