The usage of php field is: 1. Use the "$Model->field('id,title,content')->select();" method in the query operation; 2. Use the "$ Model->field(array(...))" method defines aliases etc. for some fields.
The operating environment of this article: Windows7 system, thinkphp v5.1 version, Dell G3 computer
Usage of field in php
There are many usage techniques for the field method in ThinkPHP's coherent operation method. The main purpose of the field method is to identify the fields to be returned or operated, which are discussed in detail below.
1. Used for query
The field method is the most frequently used in query operations.
$Model->field('id,title,content')->select();
Copy after login
The field method is used here to specify the values of the three fields including id, title, and content in the query result set. The executed SQL is equivalent to:
SELECT id,title,content FROM table
Copy after login
Of course, in addition to the select method, all query methods, including find, etc., can use the field method. Here we only take select as an example.
The above example can also be replaced by an array: [-more-]
$Model->field(array('id','title','content'))->select();
Copy after login
The final executed SQL is equivalent to the above.
^_^It seems that the usage of arrays is too complicated, but don’t draw this conclusion yet, you will understand the benefits of using arrays later.
The definition of array mode can define aliases for certain fields, for example:
- $Model->field(array('id','title'=>'name','content'))->select();
#The executed SQL is equivalent to:
- SELECT id,titleasname,content FROM table
##If you wish to use it directly:
$Model
- ->field('id,title as name,content')->select();## may get wrong results.
For some more complex field requirements, the advantages of arrays are more obvious, for example:
$Model
->
- field(array('id','concat(name,'-',id)'=>'truename','LEFT(title,7)'=>'sub_title'))->select();#The executed SQL is equivalent to:
SELECT id
,
concat
- (name,'-',id)astruename,LEFT(title,7)assub_title FROM table#I believe everyone understands that the array method can be a good solution for situations where SQL functions need to be used in fields.Is the field method just that useful? If you think so, you are underestimating ThinkPHP's field method. ThinkPHP considers the details far more thoughtfully than you think^_^.
Let’s look at the following situation first. If there is a table with a lot of fields and two requirements, the first requirement is to get all the fields. This may be very simple, because the field method is not called or empty is used directly. The field method can do it. In fact, it is true:
- $Model->select();
- ##$Model->field()->select();
##$Model
- ->field('*')->select();##The above three usages are equivalent and are equivalent to executing SQL:
SELECT
*
- FROM tableBut this is not what I said
Get all fields
, I If you want to explicitly call all fields (for systems with relatively high performance requirements, this requirement is not excessive, at least it is a better habit), then OK, it is still very simple. The following usage can achieve the expected effect:
$Model
->
- field(true)->select();fied(true) will explicitly obtain the list of all fields in the data table, even if you The data table has 100 fields.
The second requirement is that I want to get all field values except the content field (the value of the text field is very memory-consuming). We can use the exclusion function of the field method. For example, the following method can achieve what is said. Function:
$Model
->
- field('content',true)-> ;select();## You can also exclude more fields:
$Model
->
field
- ('user_id,content',true)->select();##//Or use
- $Model->field
(
- array('user_id','content'),true)->select();2, Used for writingIn addition to query operations, the field method also has a very important security function-field legality detection (
Note: This function can only be supported since version 3.1
). The field method combined with the create method can complete the field legality detection of form submission. If we use it in the processing method of form submission:
$Model->
field
(
- 'title,email,content')->create();means that the only legal fields in the form are title, email and content fields. No matter what means the user uses to change or add the browser's submission field, it will be directly blocked. . Since there are all other fields that we don't want to be determined by user submissions, you can define additional fields to write in via the autocomplete feature.
Summary
From the usage of field method, we should be able to understand what is called small method but big usage. Of course, we just hope that ThinkPHP will bring more convenience and surprise to your development. , welcome everyone to share your usage and experience!
Recommended learning: "
PHP Video Tutorial
"
The above is the detailed content of What is the usage of php field?. For more information, please follow other related articles on the PHP Chinese website!