Home >Backend Development >PHP Tutorial >Model two in ThinkPHP thinkphp3.1 laravel thinkphp if

Model two in ThinkPHP thinkphp3.1 laravel thinkphp if

WBOY
WBOYOriginal
2016-07-29 08:54:271035browse

Models in ThinkPHP

1. Why create data objects?

Case: Use ThinkPHP to complete department management

① Design database

thinkphp官网,thinkphp3.2,thinkphp快速入门,thinkphp视频教程,thinkphp下载,thinkphp教程,thinkphp3.1,laravel,thinkphp if

② Create Dept Controller

Path: ./Application/Admin/ControllerCreate controller

Rules: DeptController.class.php

Three steps of programming procedure

thinkphp官网,thinkphp3.2,thinkphp快速入门,thinkphp视频教程,thinkphp下载,thinkphp教程,thinkphp3.1,laravel,thinkphp if

③ Copy the add.html template to the ./Application/Admin/View/Dept folder and change the template path

④ Change View/Index/index.html left navigation

thinkphp官网,thinkphp3.2,thinkphp快速入门,thinkphp视频教程,thinkphp下载,thinkphp教程,thinkphp3.1,laravel,thinkphp if

⑤ Get the parent department that added the function (using the model)

Path: ./Application/Admin/Model/DeptModel.class.php

Three steps for programming model code:

thinkphp官网,thinkphp3.2,thinkphp快速入门,thinkphp视频教程,thinkphp下载,thinkphp教程,thinkphp3.1,laravel,thinkphp if

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

thinkphp官网,thinkphp3.2,thinkphp快速入门,thinkphp视频教程,thinkphp下载,thinkphp教程,thinkphp3.1,laravel,thinkphp if

In the template file add.html, display the list of superior departments

thinkphp官网,thinkphp3.2,thinkphp快速入门,thinkphp视频教程,thinkphp下载,thinkphp教程,thinkphp3.1,laravel,thinkphp if

⑥ Implement the data adding function addOk

In the template page, program Javascript code and submit form data

thinkphp官网,thinkphp3.2,thinkphp快速入门,thinkphp视频教程,thinkphp下载,thinkphp教程,thinkphp3.1,laravel,thinkphp if

Define form submission page

thinkphp官网,thinkphp3.2,thinkphp快速入门,thinkphp视频教程,thinkphp下载,thinkphp教程,thinkphp3.1,laravel,thinkphp if

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

thinkphp官网,thinkphp3.2,thinkphp快速入门,thinkphp视频教程,thinkphp下载,thinkphp教程,thinkphp3.1,laravel,thinkphp if

thinkphp官网,thinkphp3.2,thinkphp快速入门,thinkphp视频教程,thinkphp下载,thinkphp教程,thinkphp3.1,laravel,thinkphp if

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.

thinkphp官网,thinkphp3.2,thinkphp快速入门,thinkphp视频教程,thinkphp下载,thinkphp教程,thinkphp3.1,laravel,thinkphp if

3. Analysis of create method

Use Zend software to track the create method, as shown in the figure below:

thinkphp官网,thinkphp3.2,thinkphp快速入门,thinkphp视频教程,thinkphp下载,thinkphp教程,thinkphp3.1,laravel,thinkphp if

When the data processing is completed, the received form data will be assigned to the current object

thinkphp官网,thinkphp3.2,thinkphp快速入门,thinkphp视频教程,thinkphp下载,thinkphp教程,thinkphp3.1,laravel,thinkphp if

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:

thinkphp官网,thinkphp3.2,thinkphp快速入门,thinkphp视频教程,thinkphp下载,thinkphp教程,thinkphp3.1,laravel,thinkphp if

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:

thinkphp官网,thinkphp3.2,thinkphp快速入门,thinkphp视频教程,thinkphp下载,thinkphp教程,thinkphp3.1,laravel,thinkphp if

② Copy the index.html template to the ./Application/Admin/View/Dept folder and change the path

thinkphp官网,thinkphp3.2,thinkphp快速入门,thinkphp视频教程,thinkphp下载,thinkphp教程,thinkphp3.1,laravel,thinkphp if

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.

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