Detailed Explanation of field Method of ThinkPHP CURD Method

  • 2021-07-01 06:53:20
  • OfStack

The field method of the ThinkPHP CURD method belongs to the first coherent operation method of the model, and its main purpose is to identify the fields to be returned or operated, which can be used for query and write operations.

1. Used for query

The field method is used most frequently in query operations.


$Model->field('id,title,content')->select();

Here, the field method is used to specify the values of id, title, and content fields in the result set of the query. The executed SQL is equivalent to:


SELECT id,title,content FROM table

Of course, except select method, all query methods, including find, can use field method, and select is only used as an example.
The above example can also be replaced by arrays:


$Model->field(array('id','title','content'))->select();

The final SQL executed is equivalent to the above.

It seems that the use of arrays is too complicated, but don't make this conclusion yet, and you will understand the benefits of array use later.
The definition of array mode can define aliases for some fields, such as:


$Model->field(array('id','title'=>'name','content'))->select();

The executed SQL is equivalent to:


SELECT id,title as name,content FROM table

If you wish to use:


$Model->field('id,title as name,content')->select();

You may get wrong results.
For 1 more complex field requirements, the advantages of arrays are more obvious, such as:


$Model->field(array('id','concat(name,'-',id)'=>'truename','LEFT(title,7)'=>'sub_title'))->select();

The SQL executed is equivalent to:


SELECT id,concat(name,'-',id) as truename,LEFT(title,7) as sub_title FROM table

As we all know, the array method can solve the problem of using SQL function in field.
Is the field method so effective? If you think so, you underestimate the field method of ThinkPHP, and the details of ThinkPHP are far more thoughtful than you think.

Let's look at the following situation first. If there is a table with many fields and there are two requirements. First, it is required to get all the fields. This may be very simple, because it can be done without calling the field method or directly using the empty field method. In fact, it is true:


$Model->select();
$Model->field()->select();
$Model->field('*')->select();

The above three usages are equivalent, all of which are equivalent to executing SQL:


SELECT * FROM table

But this is not what I said to get all the fields. I want to call all the fields explicitly (for systems with high performance requirements, this requirement is not excessive, at least a better habit), so OK is still very simple, and the following usage can accomplish the expected effect:


SELECT id,title,content FROM table

0

The use of fied (true) explicitly gets a list of all the fields in a table, even if your table has 100 fields.
The second requirement is that I want to get all the field values except the content field (the value of the text field is very memory-consuming), so we can use the exclusion function of the field method, for example, the following way can achieve the said function:


SELECT id,title,content FROM table

1

To exclude more fields, you can also:


SELECT id,title,content FROM table

2


2. For writing

In addition to the query operation, the field method also has a very important security function-field validity detection (note: this function will not be supported until version 3.1). field method combined with create method can complete the field legitimacy detection of form submission. If we use it in the processing method of form submission:


$Model->field('title,email,content')->create();

That is to say, the only legal fields in the form are title, email and content fields, which will be directly masked no matter what means the user changes or adds the browser submission field. Because we don't want all other fields to be decided by user submission, you can define additional fields to write through AutoCompletion.


Related articles: