Models in ThinkPHP
1. Why create data objects?
Case: Use ThinkPHP to complete department management
① Design database
② Create Dept Controller
Path: ./Application/Admin/ControllerCreate controller
Rules: DeptController.class.php
Three steps of programming procedure
③ Copy the add.html template to the ./Application/Admin/View/Dept folder and change the template path
④ Change View/Index/index.html left navigation
⑤ Get the parent department that added the function (using the model)
Path: ./Application/Admin/Model/DeptModel.class.php
Three steps for programming model code:
Instantiate the model in the controller to complete loading the superior department function.
1) Instantiate the model
2) Call the select method
3) Assign variables to template files
In the template file add.html, display the list of superior departments
⑥ Implement the data adding function addOk
In the template page, program Javascript code and submit form data
Define form submission page
Define the addOk method in the controller to implement the add operation
Knowledge points to be used: I method, main function: receiving form data, higher security
2. How to create data objects
⑦ Introduce the create method to create data objects
Although the above addOk can add data to the database normally, it is too redundant for receiving and processing the form. We can use the create method in ThinkPHP to simplify it.
3. Analysis of create method
Use Zend software to track the create method, as shown in the figure below:
When the data processing is completed, the received form data will be assigned to the current object
Question: Why can the add method be added successfully without adding any parameters?
Answer: Continue to analyze the add method, the code is as follows:
If there are no parameters in the add method, the system will automatically read the $this->data attribute. Since the create method just now will store the form data in the $this->data attribute after the processing is completed, the system Form data is automatically added to the data table.
4. Complete the department list function
① Define the index method in the controller, as shown in the figure below:
② Copy the index.html template to the ./Application/Admin/View/Dept folder and change the path
The above introduces the second model in ThinkPHP, including the content of thinkphp. I hope it will be helpful to friends who are interested in PHP tutorials.