ThinkPHP learning-CURD operations (1)

Source: Internet
Author: User
ThinkPHP learning-CURD operations (I) written in front

Learn ThinkPHP this week and record the learning diary. We should not only learn how to use TP, but also learn the TP framework from the source code.

The diary is written every day, but not always on the Internet. I hope I can stick to it.

Let's talk about it in a few minutes. since we read the source code, we won't talk about a series of operations such as TP establishment and configuration. the official documentation provides a detailed introduction to the idea.

If you want to learn TP, click ThinkPHP3.2.3.

CURD operation

What is a CURD operation? In fact, it is short for adding, deleting, modifying, and querying databases. It includes four basic operations: create, update, read, and delete. In TP, the implementation of CURD operations is add, save, select, and update.

Open Think/Library/Model. class. php and you can find these four operations. Next we will start to view the source code and know the running process to better use these four operations.

Add method for CUED operation

The add method is used to add data. it is the TP implementation of the create operation. Note that the create method also exists in the TPModel, but it is not a kind of CURD operation, but a processing method for the data to be inserted into the database.

Parameter Analysis

The add method has three parameters (not required ).

$ Data default value '': the data to be inserted into the database $ option default value: array () expression parameters, which store allCoherent operation$ Replace: the default value of "false" indicates whether the replace operation is performed when the database is inserted.

The returned value may be bool (false) or the number of affected rows or primary key values of the database. From the return value, we can see that this operation must be at the end of a coherent operation and can be seen as an endpoint in a continuous set of techniques.

Process Analysis

The execution process of the add method is divided into four steps.

  • First, check whether the data is empty.
  • Second, data processing and expression analysis
  • All of the preceding operations return the insert result through data insertion.
  • Analyze the returned results

Step 1Assume that the User registration operation is performed, the User data table is inserted, and the add method is used for the operation. The user field is:

Primary key user_id nickname user_name password email

Front-end POST data:

array(    'user_name'     => 'xiamsahfw',    'password'      => 'adhe99211' ,    'confrim'       => 'adhe99211' ,    'email'         => '221131@qq.com',    'hid'           => 'register'              );

Actions in the UserController class:

$data = I('post.');$User = M('User');$User->create($data);$User->add();

In the add operation, no parameter is passed to add, but this value is passed in $ User-> create ($ data, in this operation, the data is automatically added to $ this-> data in the Model, and the value is automatically referenced by add.

At the same time, the create method will automatically compare the passed parameters with the fields in the data table, and delete fields not in the table.

Step 2Then, the add method will call the _ facade method to process $ data. In fact, here is the second processing of $ data, because the create method and the data have been processed. If the create method is not called in UserController, but $ data is directly passed to the add method, _ facade will set 'confrim' => 'adhe99211 ', 'hid '=> 'register' is deleted and changed:

array(     'user_name'     => 'xiamsahfw',     'password'      => 'adhe99211' ,     'email'         => '221131@qq.com'                );

For possible coherent operations, add calls _ parseOptions for expression analysis. After expression analysis, even if no coherent operation is performed, two elements exist in the returned value:

Array ([table] => user[model] => User)

Table indicates the data table to be operated, and model indicates the model name of the operation.

If a coherent operation exists, such as where and group by, it is also displayed in this item. In addition, passing a coherent operation to the add method is also merged with an existing operation.

Step 3The above are all preparations for data insertion. after the preparation is complete, data insertion can be performed. Of course, data insertion does not belong to the Model function. in TP, you need to call the insert method in Think \ Driver. class. php.

The insert method accepts three parameters.

$ Data $ option $ replace

The first two parameters are the processed data and the combined coherent operation expression. The third parameter indicates whether to perform the replace operation when the database is inserted. the default value is false. In this method, the transmitted data and the coherent operation expression are combined to form a formal SQL statement and execute the SQL statement. Finally, the execution result is returned.

Step 4Although the returned results are obtained, add does not directly return the results, but returns the analysis conclusion. The result returned by insert may include:

Failed => false succeeded => the affected number of rows is returned.

However, in practice, we may not only want to get a conclusion that the insertion is successful, but also want to get the inserted primary key value. In the add method, the returned results are judged to return the affected rows (multiple data inserts), primary key values (one data entry), or false.

Aside: I noticed such a line of code before inserting data.

if(false === $this->_before_insert($data,$options)) {    return false;}

TP does not make any comments here. after searching, it is found to be an empty method, which is interpreted as the callback method before data insertion.

// Callback method before data insertion protected function _ before_insert (& $ data, $ options ){}

Print this method and the result is NULL.

var_dump($this->_before_insert($data,$options));  // NULL

Baidu did not find the desired answer. I have not read the official TP documentation, and may have an official usage introduction. I guess this method can be used to perform some special operations (very likely) during subclass inheritance ). Similarly, the _ after_insert method is used.

Summary

I wrote a large string and I don't know whether the writing is clear, but I am actually familiar with this method. Although this method can not pass parameters, it will still fail if you haven't done create before. although data processing is integrated internally, it is not perfect. for example, the data cannot be escaped, data fields cannot be verified. we need to call other methods by ourselves. Knowing the operating principle of add can naturally make good use of it, and it also has great inspiration for encapsulating model classes.

Write it here today. The next article is about to write read in CURD, that is, select in TP.

The blogger is studying hard and has some mistakes in the article. You are welcome to correct them and criticize them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.