This article describes four methods of instantiating models in ThinkPHP, which has very important applications in ThinkPHP programming. The details are as follows:
1. Create a basic model: instantiate a database operation class that comes with the system
Test.Model.class.php page code is as follows:
class TestModel extends Model{ }
UserAction.class.php page code is as follows:
function test(){ $test=M('test');//表示实例化的是自带的Model类,并且传入test值表示操作的是test表 //等同于$test=new TestModel(); $test=$test->select(); print_r($test);//输出test表中所有数据 }
2. Instantiate a custom model
If the database operation is more complex, you need to add some custom database operation methods to the custom Model class
UserModel.class.php page code is as follows:
class UserModel extends Model{ function pyj(){ echo 'pengyanjie'; //其它的一些数据库操作方法 } }
UserAction.class.php page code is as follows:
function user(){ $user=D('User');//实例化自定义的数据库操作类 //等同于$user=new UserModel(); $user->pyj();//调用User模型中的pyj方法 }
Or, you need to instantiate a table, and at the same time, instantiate a custom database operation class written by yourself, the code is as follows:
function love(){ $love=M('test','UserModel'); //$love=new UserModel('test'); $list=$love->select(); dump($list); $love->pyj(); }
3. Instantiate a user model
UserAction.class.php page code is as follows:
function user(){ $user=new UserModel();//等同于$user=D('User'); $list=$user->select(); dump($list); echo $user->aa(); }
The UserModel.class.php page code is as follows:
The class name user corresponds to the table name user, so when instantiating this model in UserAction, there is no need to pass the additional table name. The code is as follows:
class UserModel extends Model{ function aa(){ echo 'pengyanjie'; } }
The difference between this third instantiation model method and the second one is: in your business logic, usually there will be some public business logic, then you use the second M('table name', 'Model name'); such as M('user','CommonModel') will be more convenient;
The third instantiation model method is suitable for more complex business logic for the operated table, but it does not require the use of common business logic. (Its business logic is unique to the user table and does not need to be used in other models).
4. Instantiate an empty model. It does not know which table you want to use when instantiating the operation.
$user=new Model();//等价与$user=M(); $list=$user->query('select * from think_user'); //使用传统的sql语句的方式,如果这样的话,就必须要加表前缀 dump($list);
Attachment: The difference between $user=new UserModel(); and $user=D('user');:
(1). The D method can automatically detect the model class. If it does not exist, it will throw an exception. At the same time, models that have been instantiated will not be instantiated repeatedly. The default D method can only be applied to models under the current project.
(2) If I say this is a front-end application, but I want to instantiate the model of the back-end project, I can use D.
$user=D('admin','user');//会去自动找admin分组下的user模型类
or:
$user=D('admin.user');
I hope the examples described in this article will be helpful to everyone in ThinkPHP programming.
Add your database connection information to the configuration file you generated separately.
It should be added to the config.php file in the Conf directory with your database connection information.
'URL_MODEL' =>1
Is there a comma missing after 1 in English state?
Remember to delete the runtime folder.