Field Method of ThinkPHPCURD _ php instance

Source: Internet
Author: User
The field method of the ThinkPHPCURD method mainly aims to identify the fields to be returned or operated, and can be used for query and write operations. This article mainly introduces the detailed usage of the field method. For more information, see the field method of the ThinkPHP CURD method. Identifies the fields to be returned or operatedAnd can be used for query and write operations.

1. Used for query

The field method is most frequently used in query operations.

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

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

SELECT id,title,content FROM table

Of course, in addition to the select method, all query methods, including find, can use the field method. Here we only use the select method as an example.
The preceding example can also be replaced by an array:

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

The final SQL statement is equivalent to the preceding one.

It seems that the array usage is too complicated, but don't come to this conclusion, and you will understand the advantages of array usage later.
An array can define aliases for certain fields, for example:

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

The executed SQL is equivalent:

SELECT id,title as name,content FROM table

If you want to directly use:

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

The error message may be returned.
For more complex field requirements, the advantage of arrays is more obvious, for example:

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

The executed SQL is equivalent:

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

As you may have understood, the array method can be a good solution to the need to use SQL functions in the field.
Is the field method so useful? If you think so, we should underestimate ThinkPHP's field method. ThinkPHP considers more details than you think.

Let's first look at the following situation. If there is a table with many fields and there are two requirements, you must first obtain all the fields. This may be very simple, this can be done without calling the field method or directly using the null field method. In fact, it is true that:

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

The preceding three operations are equivalent to SQL Execution:

SELECT * FROM table

However, this is not what I mean to obtain all fields. I Want To explicitly call all fields (this requirement is not too high for systems with high performance requirements, at least it is a good habit), So OK is still very simple, the following usage can accomplish the expected role:

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

Fied (true) is used to explicitly obtain the list of all fields in the data table., Even if your data table has 100 fields.
The second requirement is that I want to obtain all field values other than the excluded content field (the value of the text field is very memory-consuming), so we can use the field exclusion function, for example, the following method can implement the aforementioned functions:

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

To exclude more fields, you can also:

$ Model-> field ('user _ id, content', true)-> select (); // or use $ Model-> field (array ('user _ id ', 'content'), true)-> select ();


2. Used for writing

In addition to query operations,The field method also has a very important security feature-field validity detection (Note: This feature is available in version 3.1 ).The field method can be used in combination with the create method to check the validity of the field submitted by the form. If we use the following method in the form submission process:

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

That is, the valid fields in the form are only the title, email, and content fields. No matter how you modify or add the submitted fields in the browser, they are directly blocked. Because we do not want to determine all other fields to be submitted by the user, you can define additional field writes through the automatic completion function.

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.