field('id,title,content')->select();" in query operation; 2. Use "$Model->field(array(. ..))" method defines aliases, etc. for certain fields."> What is the usage of php field?-PHP Problem-php.cn

What is the usage of php field?

藏色散人
Release: 2023-03-12 16:02:01
Original
2183 people have browsed it

The usage of php field is: 1. Use the "$Model->field('id,title,content')->select();" method in the query operation; 2. Use the "$ Model->field(array(...))" method defines aliases etc. for some fields.

What is the usage of php field?

The operating environment of this article: Windows7 system, thinkphp v5.1 version, Dell G3 computer

Usage of field in php

There are many usage techniques for the field method in ThinkPHP's coherent operation method. The main purpose of the field method is to identify the fields to be returned or operated, which are discussed in detail below.

1. Used for query

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

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

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

SELECT id,title,content FROM table
Copy after login

Of course, in addition to the select method, all query methods, including find, etc., can use the field method. Here we only take select as an example.

The above example can also be replaced by an array: [-more-]

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

The final executed SQL is equivalent to the above.

^_^It seems that the usage of arrays is too complicated, but don’t draw this conclusion yet, you will understand the benefits of using arrays later.
The definition of array mode can define aliases for certain fields, for example:

  1. $Model->field(array('id','title'=>'name','content'))->select();
#The executed SQL is equivalent to:

  1. SELECT id,titleasname,content FROM table
  2. ##If you wish to use it directly:

    $Model
  1. ->field('id,title as name,content')->select();## may get wrong results.
  2. For some more complex field requirements, the advantages of arrays are more obvious, for example:


$Model
    ->
  1. field(array('id','concat(name,'-',id)'=>'truename','LEFT(title,7)'=>'sub_title'))->select();#The executed SQL is equivalent to:

SELECT id

,
    concat
  1. (name,'-',id)astruename,LEFT(title,7)assub_title FROM table#I believe everyone understands that the array method can be a good solution for situations where SQL functions need to be used in fields.Is the field method just that useful? If you think so, you are underestimating ThinkPHP's field method. ThinkPHP considers the details far more thoughtfully than you think^_^.
  2. Let’s look at the following situation first. If there is a table with a lot of fields and two requirements, the first requirement is to get all the fields. This may be very simple, because the field method is not called or empty is used directly. The field method can do it. In fact, it is true:
  1. $Model->select();
  2. ##$Model->field()->select();
  3. ##$Model
  4. ->field('*')->select();##The above three usages are equivalent and are equivalent to executing SQL:

SELECT
    *
  1. FROM tableBut this is not what I said
  2. Get all fields
, I If you want to explicitly call all fields (for systems with relatively high performance requirements, this requirement is not excessive, at least it is a better habit), then OK, it is still very simple. The following usage can achieve the expected effect:

$Model
    ->
  1. field(true)->select();fied(true) will explicitly obtain the list of all fields in the data table, even if you The data table has 100 fields.
  2. The second requirement is that I want to get all field values except the content field (the value of the text field is very memory-consuming). We can use the exclusion function of the field method. For example, the following method can achieve what is said. Function:


$Model
    ->
  1. field('content',true)-> ;select();## You can also exclude more fields:

$Model

->
    field
  1. ('user_id,content',true)->select();##//Or use
  2. $Model->field
  3. (
  4. array('user_id','content'),true)->select();2, Used for writingIn addition to query operations, the field method also has a very important security function-field legality detection (
  5. Note: This function can only be supported since version 3.1
). The field method combined with the create method can complete the field legality detection of form submission. If we use it in the processing method of form submission:

$Model->

field
    (
  1. 'title,email,content')->create();means that the only legal fields in the form are title, email and content fields. No matter what means the user uses to change or add the browser's submission field, it will be directly blocked. . Since there are all other fields that we don't want to be determined by user submissions, you can define additional fields to write in via the autocomplete feature.
  2. Summary

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

Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of What is the usage of php field?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!