Home >Backend Development >PHP Tutorial >ThinkPHP study notes four methods of instantiating models_PHP tutorial

ThinkPHP study notes four methods of instantiating models_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:08:251028browse

Create Action class

[php]
class NewObjectAction extends Action{
public function index(){
//1. Create a basic model
// //1: Instantiate a system database operation class
// //new Model('User') is equivalent to M('User'), placing a table that needs to be operated on in the Model
// //1 Naming:
// // thinkphp has a table name prefix by default, if it is think_user; you can use User or user to obtain it in the Model
// // If it is think_user_message; then use Model('UserMessage'); uppercase means adding a _ in front of the table name
// $User=new Model('User');
// //Call query method
// $list=$User->select();
// dump($list);
// // 2: Cross-model operation; instantiate a table and instantiate a database operation class written by yourself
//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();
//Three: Instantiate a user-defined model
//Mostly used for businesses that do not require more complexity
//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');
//D method will throw an exception if the model does not exist, and will only instance one instance. By default, it only supports calling the model under the current application
// $user=new UserModel();
// $list=$user->select();
// dump($list);
// $user->modelTest();
//4. Instantiate an empty model; it is the way of traditional sql statement; table prefix needs to be added
//
$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 operation, call the method in the model';
} }
}
?>
[php]
class UserModel extends Model{
function modelTest(){
Echo 'test cross -model operation, call the method in the model';
} }
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477793.htmlTechArticleCreate Action class [php] ?php class NewObjectAction extends Action{ public function index(){ //1. Create A basic model // //1: Instantiate a system database operation class // //new...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn