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...