Home  >  Article  >  Backend Development  >  Detailed explanation of getField in ThinkPHP_PHP tutorial

Detailed explanation of getField in ThinkPHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:24:19841browse

The getField method is a method used in ThinkPHP to obtain field values. It is different from the select and find methods. It is usually only used to obtain the value of individual fields. But in fact it is not that simple. The usage of this method is summarized as follows:

1. Get a field value

This is the most basic usage of the getField method, which is used to obtain a field value that meets the conditions.

$User = M("User"); // 实例化User对象
 // 获取ID为3的用户的昵称 
$nickname = $User->where('id=3')->getField('nickname');

The returned nickname is a string result. That is, even if there are multiple fields that meet the condition, only one result will be returned.

2. Get a certain field column

If you want to return field columns (multiple results) that meet the requirements, you can use:

$User = M("User"); // 实例化User对象
 // 获取status为1的用户的昵称列表
$nickname = $User->where('status=1')->getField('nickname',true);

The second parameter is passed in true, and the returned nickname is an array containing a list of all nicknames that meet the conditions.

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

$nickname = $User->where('status=1')->getField('nickname',8);

or

$nickname = $User->where('status=1')->limit(8)->getField('nickname',true);


3. Get 2 field lists

If you want to get a list of IDs and nicknames that meet the conditions, you can use:

$User = M("User"); // 实例化User对象
 // 获取status为1的用户的昵称列表
$nickname = $User->where('status=1')->getField('id,nickname');

If the getField method passes in multiple field names, an associative array will be returned by default, with the value of the first field as the index (so the first field should be chosen as non-duplicate as possible).
You can also limit the number of returns, for example:

$nickname = $User->where('status=1')->getField('id,nickname',8);

or

$nickname = $User->where('status=1')->limit(8)->getField('id,nickname');


4. Get multiple field lists

If more than 2 field names are passed in, a two-dimensional array will be returned (similar to the return value of the select method, the difference is that the index is the key name of the two-dimensional array is the value of the first field), for example:

$result = $User->where('status=1')->getField('id,account,nickname');

If you do not want to return a two-dimensional array, you can use the connector, for example:

$result = $User->where('status=1')->getField('id,account,nickname',':');

Then the returned result is an index array, the key name is the id value, and the key value is a string composed of account:nickname concatenation.

The getField method can usually be used in conjunction with consecutive operations such as where, limit, order, etc. There are also some advanced usages, including support for join tables and sql methods.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825497.htmlTechArticleThe getField method is a method used to obtain field values ​​in ThinkPHP. It is different from the select and find methods and is usually only used for Get the value of an individual field. But in fact it’s not that simple. Now it’s time to...
Statement:
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