Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of ThinkPHP CURD's field method

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you about the usefulness of ThinkPHP CURD's field method. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The field method of the ThinkPHP CURD method is one of the coherent operation methods of the model, and its 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 most frequently used in query operations.

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

The field method is used here to specify the values of the three fields id,title,content 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. Here we just take select as an example.

The above example can also use an array instead:

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

The final SQL executed is equivalent to the above.

It seems that the use of arrays is too complex, but don't draw this conclusion yet, and you'll see the benefits of array usage later.

The definition of array mode can define aliases for some fields, such as:

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

The SQL executed is equivalent to:

SELECT id,title as name,content FROM table

If you want to directly use:

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

You may get the wrong results.

For some of the more complex field requirements, the advantages of arrays are more obvious, such as:

$Model- > field (array ('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

As you all know, the array approach is a good solution for situations where you need to use the SQL function in field.

Is that all the field method does? If you think so, you underestimate ThinkPHP's field approach. ThinkPHP is far more considerate of the details than you think.

Let's take a look at the following situation. If a table has a lot of fields and has two requirements, it may be simple to get all the fields first, because you can do it without calling the field method or directly using an empty field method. In fact, it is:

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

The above three uses are equivalent and are equivalent to performing SQL:

SELECT * FROM table

But this is not what I said to get all fields, I want to explicitly call all fields (for systems with high performance requirements, this requirement is not excessive, at least a good habit), then OK, is still very simple, the following usage can achieve the desired effect:

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

The use of fied (true) explicitly gets a list of all fields in a datasheet, even if your datasheet 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), so we can use the exclusion feature of the field method, for example, in the following ways:

$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. For writing

In addition to query operations, the field method also has a very important security feature-field validity detection (note: this feature will not be supported until version 3.1). The field method can be used in conjunction with the create method to check the validity of the field submitted by the form, if we use the following in 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, which will be directly blocked no matter what means the user changes or adds the submitted field of the browser. Because we don't want all the other fields to be decided by the user's submission, you can define additional field writes through the autocomplete feature.

Thank you for reading! This is the end of this article on "what is the use of ThinkPHP CURD's field method". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 269

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report