GetField of ThinkPHP _ php instance

Source: Internet
Author: User
This article mainly introduces the getField method of ThinkPHP. You can refer to the getField method in ThinkPHP to obtain the field value, which is different from the select and find methods, it is usually used only to obtain the values 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. It is used to obtain a field value that meets the condition.

$ User = M ("User"); // instantiate the User object // obtain the nickname of a User whose ID is 3 $ nickname = $ User-> where ('Id = 3 ') -> getField ('nickname ');

The returned nickname is a string. That is to say, even if multiple fields meet the conditions, only one result is returned.

2. Obtain a field Column

If you want to return a qualified field column (multiple results), you can use:

$ User = M ("User "); // instantiate the User object // obtain the nickname list of users whose status is 1 $ nickname = $ User-> where ('status = 1')-> getField ('nickname ', true );

If the second parameter is set to true, the returned nickname is an array that contains a list of all nicknames that meet the conditions.

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. Obtain the list of two fields

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

$ User = M ("User "); // instantiate the User object // obtain the nickname list of users whose status is 1 $ nickname = $ User-> where ('status = 1')-> getField ('Id, nickname ');

If the getField method is used to input multiple field names, an associated array is returned by default, and the value of the first field is the index (so the first field should be selected as unique as possible ).
You can also limit the number of returned results, for example:

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

Or

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


4. Obtain the list of Multiple Fields

If more than two field names are input, a two-dimensional array is 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 and 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 a connector, for example:

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

The returned result is an index array with the key name being the id value and the key value being a string consisting of account: nickname connections.

The getField method can also be used together with the where, limit, and order operations. There are also some advanced usage, including support for join tables and SQL methods.

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.