CURD method check: field method

Source: Internet
Author: User
There are many very practical methods in the CURD operations of ThinkPHP. we will introduce them one by one from this article. There are many very practical methods in the CURD operations of ThinkPHP. we will introduce them one by one from this article.

First, we will introduce the usage of the field method. A field is one of the model's coherent operations. it is used to identify the fields to be returned or operated, and can be used for query and write operations.

1. The field method is most frequently used in queries.
  1. $ Model-> field ('Id, title, content')-> select ();
Copy the code. here, 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:
  1. SELECT id, title, content FROM table
Copy the code. 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:
  1. $ Model-> field (array ('id', 'title', 'Content')-> select ();
The final SQL statement executed by the copied code 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:
  1. $ Model-> field (array ('id', 'title' => 'name', 'Content')-> select ();
The SQL statement executed by copying the code is equivalent:
  1. SELECT id, title as name, content FROM table
Copy the code if you want to directly use:
  1. $ Model-> field ('Id, title as name, content')-> select ();
Copying code may produce incorrect results.
For more complex field requirements, the advantage of arrays is more obvious, for example:
  1. $ Model-> field (array ('id', 'concat (name, '-', id) '=> 'truename', 'Left (title, 7) '=> 'sub _ title')-> select ();
The SQL statement executed by copying the code is equivalent:
  1. SELECT id, concat (name, '-', id) as truename, LEFT (title, 7) as sub_title FROM table
Everyone must understand how to copy the code. the array method can be used to solve the problem of using SQL functions in the field.
Is the field method so useful? If you think so, ThinkPHP's field method is too underestimated. 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:
  1. $ Model-> select ();
  2. $ Model-> field ()-> select ();
  3. $ Model-> field ('*')-> select ();
The preceding three operations of the Copy code are equivalent to executing the SQL statement:
  1. SELECT * FROM table
Copy the code, but this is not what I said. Retrieve all fields, I want to explicitly call all fields (for systems with high performance requirements, this requirement is not too much, at least a good habit), so OK is still very simple, the following usage can accomplish the expected purpose:
  1. $ Model-> field (true)-> select ();
The usage of the Copy code fied (true) will 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:
  1. $ Model-> field ('content', true)-> select ();
To exclude more fields, you can also copy the code:
  1. $ Model-> field ('User _ id, content', true)-> select ();
  2. // Or use
  3. $ Model-> field (array ('User _ id', 'Content'), true)-> select ();
Copy code
2. in addition to query operations, the field method also has a very important security function-field validity detection ( Note: This function is supported only 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:
  1. $ Model-> field ('title, email, content')-> create ();
Copy the code to indicate that 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.

Summary from the usage of the field method, we should be able to understand what is the big usage of small methods. of course, we just hope ThinkPHP will bring more convenience and surprise to your development, you are welcome 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.