fields() public method
Returns the list of fields that should be returned by default by toArray() when no specific fields are specified.
A field is a named element in the returned array by toArray().
This method should return an array of field names or field definitions. If the former, the field name will be treated as an object property name whose value will be used as the field value. If the latter, the array key should be the field name while the array value should be the corresponding field definition which can be either an object property name or a PHP callable returning the corresponding field value. The signature of the callable should be:
function ($model, $field) { // return field value }
For example, the following code declares four fields:
-
email
: the field name is the same as the property nameemail
; -
firstName
andlastName
: the field names arefirstName
andlastName
, and their values are obtained from thefirst_name
andlast_name
properties; -
fullName
: the field name isfullName
. Its value is obtained by concatenatingfirst_name
andlast_name
.
return [ 'email', 'firstName' => 'first_name', 'lastName' => 'last_name', 'fullName' => function () { return $this->first_name . ' ' . $this->last_name; }, ];
In this method, you may also want to return different lists of fields based on some context information. For example, depending on the privilege of the current application user, you may return different sets of visible fields or filter out some fields.
The default implementation of this method returns the public object member variables indexed by themselves.
See also toArray().
public array fields ( ) | ||
---|---|---|
return | array |
The list of field names or field definitions. |
Please login to continue.