TP Review

【Foreword】

    After using the TP framework for a period of time, I found that querying the manual is actually a necessary skill. Because it is impossible to remember all, and the version changes very quickly. Here is a summary of the difficulties and doubts in the recent application of TP3.2.3

 

【main body】

 (1) D method

The D method is a method used to call the database table name after customizing the data model. You use the same method as M. The difference is that M only calls the system Model, and U calls the Model you defined.

(2) I method acquisition and POST acquisition

I('get.id') and I('post.'), I method with filter effect

(3) Auxiliary methods

(4) Coherent operation

(5) Joint table query

(6) Upload time during upload/add operation

if(IS_POST){//Process submission
                // Receive fields other than file, because file cannot be received by post
                $post = I('post.');
                //Complete the addtime field
                $post['addtime']=time();
                // instantiate the model
                $model = M('doc');
                $result = $model->add($post);
                // Judge the save result
                if($result){
                    $this->success('Added successfully',U('showList'),3);
                }else{
                    $this-error('Add failed');
                }
            }else{//Display template
                $this->display();
            }

   expand***:

   The read time in the template also needs to be converted into a format

{$vol.addtime|date='Y-m-d H:i:s',###}

(7) Fast methods used so far: U (URL assembly), D (instantiate custom model), M (instantiate parent model), G (performance test)

  ①G syntax: If the third parameter is a number, it indicates the execution time of the statistical code, and the number indicates the exact number of decimal places (unit: us microseconds---1 second=1000 milliseconds=1000000 microseconds); if it is the character m , it means the statistical memory overhead (unit: byt), which requires the support of the server

G('start mark')
 //The code segment that needs statistical efficiency
G('end marker')
G('start marker', 'end marker', 'number/character')

Case: Use the G method to count the execution time overhead of a certain piece of code

public function test(){
            //define start tag
            G('start');
            // code snippet
            for($i=0;$i<100000;$i++){
                // echo $i;
            }
            // end tag
            G('stop');
            //Start statistics, 4 means accurate to the last 4 decimal places
            echo G('start','stop',4);
}

 The output after the test: 0.0030, so it took 0.0030 seconds to execute

   ②I method

Handling form submissions

The add method in the controller deptController.class.php determines whether it is a POST submission. Submit if so, otherwise display

Extension:

          1. Method judgment --------- Judging whether the request is a post, before using $_POST to judge, but in ThinkPHP, the system encapsulates several more practical constants, which can be directly judged by constants

             IS_POST、IS_GET、IS_AJAX、IS_CGI、IS_PUT等

          2. Data reception---------Before using $_POST to receive data, use the I fast method in ThinkPHP to receive data. The I method can receive any type of input post, get, request, put, etc. And the system defaults to prevent SQL

The injection method (using the PHP built-in function htmlspecialchars, converts the predefined characters <,> into HTML entity characters). Because $POST does not filter the data, it has to be filtered manually. So it is recommended to use the I method to receive data,

Built-in filtering effect to prevent SQL injection

         3.I quick method syntax: I('variable type.variable name', ['default value'], ['filter method']), the filter method can be customized to filter

            Receive the entire array: I('get');

public function add(){
            / / Determine the request type, if it is post, submit it, otherwise display it
            if (IS_POST) {
                // handle the form request
                $post = I('post.');
                //After successful reception, write data
                $model = M('dept');//Model instantiation
                $result = $model->add($post);
                // determine the return value
                if ($result) {
                    $this->success('Added successfully',U('showList'),3);
                }else{
                    $this->error('add failed');//skip to the previous page by default
                }
            }else{
                $model = M('dept');//Model instantiation
                $data = $model->where('pid = 0')->select();//Conditional query
                $this->assign('data',$data);//The variable assignment is passed to the template
                $this->display();
            }
        }

  ③U method: refer to http://www.cnblogs.com/jianxian/p/8637061.html

 

(8) Encapsulation by intercepting strings, sending get and post requests by curl, removing spaces, etc.

Reference URL: http://www.cnblogs.com/jianxian/p/8689105.html

 

(9) Upload error

    After viewing the source code, I found an upload error, so here is a summary of upload errors 0-7

0--------------- No errors, upload successful
1---------------The uploaded file exceeds the limit of the upload_max_filesize option in php.ini!
2---------------The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form!
3---------------The file is only partially uploaded!
4------------- No files were uploaded!
6--------------- Temporary folder not found!
7-------------File writing failed!

 

(10)MVC心得->如果可以直接进行CURD操作的,简单的基本操作可以直接在控制器编写。如果数据需要保存处理等操作,则最好放到模型里,进行数据的CURD操作。

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326156410&siteId=291194637