Create data Object 1, why do you want to create a data object
In the actual project development, often to receive form data, if the form data too much, we need to receive the process through the $_post, in order to simplify the above operation, we can use the data object to solve the problem.
?
Thinkphp can help you create data objects quickly, and the most typical application is to automatically create data objects based on form data, which is especially noticeable in the case of a very high number of fields in a single data table.
2. Simulating the creation of data objects
① changing form data in a add.html template page
② Set drop-down check box
The following is defined in the Add method of the controller:
Traverse through volist in the template:
③ defines a form form that wraps all the form elements as follows:
④ submitting form data using AJAX
⑤ on the server to receive the form data
⑥ the client to determine whether to add success
Question: Why do we not receive any data, just use the Create method and the Add method to complete the receipt of the form and the addition of data?
3, create method detailed
Knowledge points to use: I method
Main function: For receiving form data, equivalent to $_get and $_post
Why use the I method instead of $_get and $_post? Answer: Because I method is more secure.
The Create method is detailed:
If there are no parameters in the Create method, the system receives all the data in the $_post array by default, and then after a series of operations, assigns the returned result to the current object's Data property.
Question: Why is there no parameter in the Add method that can be added normally?
A: The main reason is that the Add method also automatically determines the parameters, as shown in:
Case: Show Department list
① define the Index method in the Dept controller to display the Department list
② Copy the index.html template file to the./application/admin/view/dept folder and change the resource path
③ traversing a list of information in a template file through the Volist method
Creating Data Objects