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 ', ': ');