Thinkphp in GetField () and field ()

Source: Internet
Author: User

do database query time, more often use these two, always check manual, remember, now summarize it, hope later use when do not look up manual. Whether you query a dataset with select or query data with find, it is often used with coherent operations where, field, order, and so on. Field (): Used to define the field to query (support field exclusions) Usage: field ($field, $except=false)  parameter: $field--field name, field name to query;       '*'  ) equivalent. To explicitly pass in all fields, you can use the following method:
$model, field (True)->select ();
However, it is recommended that you only get field names that need to be explicit, or that are defined by field exclusions, for example:
$model->field (' status ', True)->select ();    Get all fields except status $model->field (' id ', ' nickname ')->select ();  Get ID and Nickname fields

GetField (): Used to query a field's value usage: GetField ($field, $sepa = NULL parameters: $field--The field string to get (multiple comma-delimited) $sepa-the field data interval symbol, or null to return an array of arrays. The default is a null return value: If the query result is NULL, returns null if field is a field and returns the value of the field if field is more than one field and returns an array. The index of the array is the value of the first field, and Sepa is null to return a two-dimensional array. Example: When there is only one field, a value is returned by default.

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

 Example: If you pass in multiple fields, the default is to return an associative array, the returned list is an array, the key name is the user's ID, and the key value is the user's nickname nickname.

$User = M ("User"); Instantiate the User object//Get the ID and nickname list of all users $list = $User->getfield (' id,nickname ');
$list = $User->getfield (' Id,nickname,email ');
It returns a two-dimensional array, similar to the return result of the Select method, except that the key name of the two-dimensional array is the user's ID (exactly: the first field name of the GetField method). Example: If we pass in a string delimiter:
$list = $User->getfield (' Id,nickname,email ', ': ');
Then the result of the return is an array, the key name is the user ID, and the key value is the output string of Nickname:email. Additionally: the SEPA parameter can also support limit quantities, which can be used in conjunction with the order method. For example:
$this->getfield (' Id,name ', 5); Limit returns 5 records $this->getfield (' ID ', 3); Get ID Array limit 3 records

Thinkphp GetField () and field ()

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.