thinkphp Development Framework function Detailed: GetField Usage Summary

Source: Internet
Author: User
Keywords Development framework function GetField usage thinkphp
Tags aliyun array basic development development framework example find framework

The GetField method is the method used in thinkphp to get the value of a field, distinguished from the Select and find methods, and is typically used only to get the values of individual fields. But the fact is not that simple, the use of the method is summarized as follows:
&http://www.aliyun.com/zixun/aggregation/37954.html ">NBSP;

Get a field value

This is the most basic use of the GetField method to get a field value that meets the criteria. The returned nickname is a string result. That is, even if there are multiple fields that meet the criteria, only one result is returned.

$User = M ("User"); Instantiate the User object//Get the nickname of the person with ID 3 = $User->where (' id=3 ')->getfield (' nickname ');

Get a field column

If you want to return the required character Chelle (multiple results), you can use: The second argument passes in true, and the returned nickname is an array containing all the nickname lists that meet the criteria.

If you need to limit the number of returned results, you can use: or

$User = M ("User"); Instantiate the User object//Get the nickname list of the users with status 1 = $User->where (' Status=1 ')->getfield (' nickname ', true); $nickname = $User->where (' Status=1 ')->getfield (' nickname ', 8); $nickname = $User->where (' Status=1 ')->limit (8)->getfield (' nickname ', true);

Get 2 field lists

If you want to get a list of IDs and nicknames that meet the criteria, you can use: If the GetField method passes in more than one field name, the default returns an associative array, indexed by the value of the first field (so try to choose not to repeat the first field).
You can also limit the number of returns, for example: or

$User = M ("User"); Instantiate the User object//Get the nickname list of the users with status 1 = $User->where (' Status=1 ')->getfield (' Id,nickname '); $nickname = $User->where (' Status=1 ')->getfield (' Id,nickname ', 8); $nickname = $User->where (' Status=1 ')->limit (8)->getfield (' Id,nickname ');

Get more than one field list

If more than 2 field names are passed in, returns a two-dimensional array (similar to the return value of the Select method, except that the index is the value of the first field for the key name of the two-dimensional array), for example: If you do not want to return a two-dimensional array, you can use a connector, for example: Then the result returned is an array of indices, The key name is the ID value, and the key value is the string composed of the Account:nickname connection.

The GetField method can often be used in conjunction with a coherent operation where, limit, order, etc. There are also advanced uses, including support for join tables and support for SQL methods.

$result = $User->where (' Status=1 ')->getfield (' Id,account,nickname '); $result = $User->where (' Status=1 ')->getfield (' Id,account,nickname ', ': ');
Related Article

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.