Usage of field in thinkphp

Source: Internet
Author: User

There are many usage techniques for the field method in Thinkphp's coherent operation method, and the field method mainly aims to identify the fields to be returned or manipulated, as detailed below.    1, for querying in the query operation field method is the most frequently used.  $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.  Execute SQL equivalent to: Select Id,title,content from Table of course, all query methods, including find, can use the field method in addition to the Select method, which is illustrated in the example of select.  The above example can also use arrays instead: $Model->field (' 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 array mode can define aliases for some fields, for example: $Model->field (' id ', ' title ' = ' name ', ' content ')->select (); Execute SQL equivalent to: SELECT id,title as name,content from table if you want to use directly: $Model->field (' id,title as Name,content ')->s  Elect ();  You may get the wrong result. For some of the more complex field requirements, the advantages of the array are more pronounced, for example: $Model->field (' 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 presumably everyone understands that for those that need to use SQL functions in field  case, the array way can be solved very well. Is that how the field method works??  If you think so, it is too underestimated thinkphp field method, thinkphp consider the details far more thoughtful than you think ^_^. 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: $Model->select  ();  $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'm talking about. 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 ();  The use of fied (true) will explicitly get a list of all the fields in the data table, even if your data 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-intensive), and we can use the exclusion of the field method, such as the following to achieve the function: $Model->field (' content ',  true)->select ();  To exclude more fields, you can also: $Model->field (' user_id,content ', True)->select ();  or with $Model->field (Array (' user_id ', ' content '), true)->select (); 2, for writing in addition to the query operation, the field method also has a very important security function--field legality detection (note: The function 3.1 version to support).  The field method is used in conjunction with the Create method to complete the validation of the form submission fields, if we are using the form submission processing method: $Model->field (' title,email,content ')->create (); This means that the legal fields in the form are only title,email and content fields, and no matter what means the user changes or adds the browser's submission field, it will directlyShielding.   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.

Usage of field in thinkphp

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.