Summary of the usage of the Thinkphp field method, and perhaps you don't know

Source: Internet
Author: User
Tags define array arrays query

The field method in Thinkphp's coherent operations method has many tricks, the main purpose of the field method is to identify the fields to return or manipulate, as detailed below.

1, for Query

The field method is the most frequently used in query operations.

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

This uses the field method to specify the values in the query's result set that contain id,title,content three fields. The SQL executed is equivalent to:

    1. SELECT id,title,content from table

Of course, in addition to the Select method, all query methods, including find, can use the field method, which is only illustrated by the select example.
The example above can also use an array instead:

    1. $Model->field (Array (' ID ', ' title ', ' content '))->select ();

The final execution of the SQL is equivalent to the above.
^_^ seems to be too complex to use for arrays, but let's not get to the conclusion, and then you'll see the benefits of array usage.
The definition of an array method can define aliases for some fields, for example:

    1. $Model->field (Array (' ID ', ' title ' => ' name ', ' content '))->select ();

The SQL executed is equivalent to:

    1. SELECT Id,title as name,content from table

If you want to use it directly:

    1. $Model->field (' id,title as Name,content ')->select ();

May get the wrong result.
For some of the more complex field requirements, the advantages of arrays are more pronounced, for example:

    1. $Model->field (Array (' ID ', ' concat (name, '-', id) ' => ' truename ', ' Left (title,7) ' => ' Sub_title '))->select ( );

The SQL executed is equivalent to:

    1. SELECT id,concat (name, '-', id) as Truename,left (title,7) as sub_title from table

It must be understood that the array approach is well resolved for situations where SQL functions need to be used in field.
is the field method so useful? If you think so, it is too underrated thinkphp's Field method, thinkphp the details of the consideration is far more thoughtful than you think ^_^.
Looking at the following scenario, if you have a table with a lot of fields and two requirements, first you need to get all the fields, this may be simple, because you can do this without calling the field method or by using the Null field method directly, indeed:

    1. $Model->select ();
    2. $Model->field ()->select ();
    3. $Model->field (' * ')->select ();

The above three usages are equivalent, and are equivalent to executing sql:

    1. SELECT * FROM table

But that's not what I said. Get all the fields, I want to call all the fields explicitly (for systems with higher performance requirements, not too much, at least a good habit), OK, still very simple, the following usage can accomplish the desired effect:

    1. $Model->field (True)->select ();

The use of fied (true) explicitly gets a list of all the fields in the datasheet, even if your datasheet has 100 fields.
The second requirement is that I want to get all the field values that exclude the content field (the value of the text field is very memory), and we can use the exclusion feature of the field method, such as the following to achieve the function described:

    1. $Model->field (' content ', true)->select ();

To exclude more fields, you can also:

    1. $Model->field (' user_id,content ', True)->select ();
    2. or use
    3. $Model->field (Array (' user_id ', ' content '), true)->select ();

2. For writing

In addition to query operations, the field method has a very important security feature--field legality detection (note: This feature version 3.1 starts to support). The field method, combined with the Create method, can be used to complete the validation of the fields of the form submission, if we use it in the processing method of the form submission:

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

That means that the legal field in the form has only title,email and content fields, and no matter what means the user changes or adds the browser's submit field, it will be shielded directly. Because, the other is all the fields we do not want to be submitted by the user to decide, you can define the additional field writes by the AutoComplete feature.

Summarize

From the use of the field method we should be able to understand what is called the small method of large usage, of course, we just hope that thinkphp to your development to bring more convenience and surprise, welcome you to share your usage and experience!



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.