PHP Trait yii\base\ArrayableTrait

ArrayableTrait implements ArrayableTrait::toArray by respecting the field definitions as declared in ArrayableTrait::fields and ArrayableTrait::extraFields.
Since: 2.0
Author: Qiang Xue ([email protected])
Afficher le fichier Open project: yiisoft/yii2

Méthodes publiques

Méthode Description
extraFields ( ) : array Returns the list of fields that can be expanded further and returned by ArrayableTrait::toArray.
fields ( ) : array Returns the list of fields that should be returned by default by ArrayableTrait::toArray when no specific fields are specified.
toArray ( array $fields = [], array $expand = [], boolean $recursive = true ) : array Converts the model into an array.

Méthodes protégées

Méthode Description
resolveFields ( array $fields, array $expand ) : array Determines which fields can be returned by ArrayableTrait::toArray.

Method Details

extraFields() public méthode

This method is similar to ArrayableTrait::fields except that the list of fields returned by this method are not returned by default by ArrayableTrait::toArray. Only when field names to be expanded are explicitly specified when calling ArrayableTrait::toArray, will their values be exported. The default implementation returns an empty array. You may override this method to return a list of expandable fields based on some context information (e.g. the current application user).
See also: toArray()
See also: fields()
public extraFields ( ) : array
Résultat array the list of expandable field names or field definitions. Please refer to [[fields()]] on the format of the return value.

fields() public méthode

A field is a named element in the returned array by ArrayableTrait::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: php function ($model, $field) { return field value } For example, the following code declares four fields: - email: the field name is the same as the property name email; - firstName and lastName: the field names are firstName and lastName, and their values are obtained from the first_name and last_name properties; - fullName: the field name is fullName. Its value is obtained by concatenating first_name and last_name. php 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 fields ( ) : array
Résultat array the list of field names or field definitions.

resolveFields() protected méthode

This method will check the requested fields against those declared in ArrayableTrait::fields and ArrayableTrait::extraFields to determine which fields can be returned.
protected resolveFields ( array $fields, array $expand ) : array
$fields array the fields being requested for exporting
$expand array the additional fields being requested for exporting
Résultat array the list of fields to be exported. The array keys are the field names, and the array values are the corresponding object property names or PHP callables returning the field values.

toArray() public méthode

This method will first identify which fields to be included in the resulting array by calling ArrayableTrait::resolveFields. It will then turn the model into an array with these fields. If $recursive is true, any embedded objects will also be converted into arrays. If the model implements the [[Linkable]] interface, the resulting array will also have a _link element which refers to a list of links as specified by the interface.
public toArray ( array $fields = [], array $expand = [], boolean $recursive = true ) : array
$fields array the fields being requested. If empty, all fields as specified by [[fields()]] will be returned.
$expand array the additional fields being requested for exporting. Only fields declared in [[extraFields()]] will be considered.
$recursive boolean whether to recursively return array representation of embedded objects.
Résultat array the array representation of the object