Thinkphp Curd Method Counting: Field method

Source: Internet
Author: User

Thinkphp's curd operation has many very practical methods, from this beginning, we will introduce to you.

First, we introduce the use of the field method. field is one of the consistent operations of the model, and the main purpose is to identify the fields to be returned or manipulated, which can be used for query and write operations.

1, for Query

The field method is the most frequently used in a query operation.

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

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

SELECT id,title,content from table

Of course, except for the Select method, all query methods, including find, can use the field method, which is illustrated in the example of select.
The above example can also use arrays instead:

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

Finally executes the SQL and is equivalent above.
^_^ seems to seem to be too complicated to use arrays, but let's not conclude that the benefits of array usage will be understood later.
The definition of an array can define aliases for some fields, for example:

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

The SQL executed is equivalent to:

 as name,content from table

If you want to use it directly:

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

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

$Model->field (array(' id ', ' concat (name, '-', id) ' = ' truename ', ' Left (title,7) ' = ' sub_ Title '))->select ();

The SQL executed is equivalent to:

 as  as sub_title from table

It must be understood that the array approach can be well solved for situations where you need to use SQL functions in field.
is the field method so useful? If you think so, it is too underestimated thinkphp field method, thinkphp consider the details far more thoughtful than you think ^_^.

First look at the following situation, if there is a table with a very many fields, and there are two requirements, the first requirement to get all the fields, this may be very simple, because do not call the field method or directly using the empty field method can do, in fact, it is true:

$ModelSelect (); $Model->field (),Select (); $Model->field (' * ')->select ();

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

SELECT * FROM table

But that's not what I said. get all fields , I want to explicitly call all fields (for a system with high performance requirements, this requirement is not too much, at least a good habit), then OK, still very simple, the following usage can accomplish the expected effect:

$Model->field (true)->select ();

To exclude more fields, you can also:

$Model->field (' User_id,content ',true)// or with $Model ->field (array(' user_id ', ' content '),true)->select ();
2. For writing

In addition to the query operation, the field method has a very important security feature-the validation of fields ( Note: This feature is supported at the beginning of version 3.1 ). The field method, combined with the Create method, can be used to detect the legality of the form submission, if we use it in the processing method of the form submission:

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

This means that the legal fields in the form are only title,email and content fields, and are masked directly, no matter what means the user changes or adds a browser's submission field. Because, the other is all the fields we do not want to be submitted by the user to decide, you can define additional fields to write through the auto-completion function.

Summarize

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

Via:http://www.thinkphp.cn/document/311.html

Thinkphp Curd Method Counting: Field method

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.