Overview of four methods of thinkphp instantiation model, thinkphp four methods
This paper describes four methods of thinkphp instantiation model, which is very important for thinkphp program design. Specific as follows:
1. Create a basic model: Instantiate a system's own database operation class
The Test.Model.class.php page code is as follows:
Class Testmodel extends model{ }
The UserAction.class.php page code is as follows:
function test () { $test =m (' Test '),//means that the instantiated is a self-contained model class, and the incoming test value indicates that the operation is the test table //equivalent to $test=new Testmodel (); $test = $test->select (); Print_r ($test);//output all data in test table
2. Instantiate a custom model
If the database operation is complex, you need to add some custom database operation methods to the custom model class
The UserModel.class.php page code is as follows:
Class Usermodel extends model{ function Pyj () { echo ' Pengyanjie '; Some other methods of database operation } }
The UserAction.class.php page code is as follows:
function User () { $user =d (' user ');//Instantiate a custom database operation class //equivalent to $user=new Usermodel (); $user->pyj ();//Call the Pyj method in the user model
Or, you need to instantiate a table, and instantiate a custom database operation class that you write yourself, with the following code:
function Love () { $love =m (' Test ', ' Usermodel '); $love =new usermodel (' test '); $list = $love->select (); Dump ($list); $love->pyj ();
3. Instantiate a user model
The UserAction.class.php page code is as follows:
function User () { $user =new usermodel ();//equivalent to $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 there is no need for an additional table name when instantiating the model in Useraction, the code is as follows:
Class Usermodel extends model{ function aa () { echo ' Pengyanjie '; } }
The third kind of instantiation model differs from the second: in your business logic, there are usually some common business logic, and you use the second m (' table name ', ' model name '), such as M (' user ', ' Commonmodel ');
The third instantiation model approach is appropriate for more complex business logic for the table being manipulated, but it does not require the use of public business logic. (Its business logic, for the user table, is unique and does not need to be used in other models).
4. Instantiate an empty model that does not know which table you are using to instantiate the operation.
$user =new Model ();//equivalence with $user=m (); $list = $user->query (' select * from Think_user '); Using a traditional SQL statement, if so, you have to add a table prefix dump ($list);
Attached: $user =new Usermodel (), and $user=d (' user '), the difference:
(1), the D method can automatically detect the model class, when it does not exist, it throws an exception. Also, for models that have been instantiated, the instantiation is not repeated. The default D method can only be applied to the model below the current project.
(2), if said, I this is the foreground application, but I want to instantiate the background project model can be done with D.
$user =d (' admin ', ' user ');//will go to automatically find the user model class under the Admin group
Or:
$user =d (' Admin.user ');
It is hoped that the examples described in this article will help you thinkphp program design.
thinkphp instantiation of Model problems
Add the information you connect to the database in your own generated configuration file.
This should be the config.php file in the Conf directory, plus the information you connected to the database.
Just contact thinkphp, I instantiate the model class, the error, the following is my code, please help me to see,
' Url_model ' =>1
is not the comma in the English state after 1.
Remember to delete the runtime folder.
http://www.bkjia.com/PHPjc/868236.html www.bkjia.com true http://www.bkjia.com/PHPjc/868236.html techarticle Overview of four methods of thinkphp instantiation model, thinkphp Four methods This paper describes the four methods of thinkphp instantiation model, which is very important for thinkphp program design. ...