没有命名空间的类 yii yii\base yii\behaviors yii\caching yii\captcha yii\console yii\console\controllers yii\console\widgets yii\data yii\db yii\db\conditions yii\db\cubrid yii\db\cubrid\conditions yii\db\mssql yii\db\mssql\conditions yii\db\mysql yii\db\oci yii\db\oci\conditions yii\db\pgsql yii\db\sqlite yii\db\sqlite\conditions yii\di yii\filters yii\filters\auth yii\grid yii\helpers yii\i18n yii\log yii\mail yii\mutex yii\rbac yii\rest yii\test yii\validators yii\web yii\widgets

Interface yii\base\Arrayable

实现yii\base\DynamicModel, yii\base\Model, yii\data\ActiveDataFilter, yii\data\DataFilter, yii\db\ActiveRecord, yii\db\BaseActiveRecord
可用版本自2.0
源码 https://github.com/yiichina/yii2/blob/api/framework/base/Arrayable.php

Arrayable is the interface that should be implemented by classes who want to support customizable representation of their instances.

For example, if a class implements Arrayable, by calling toArray(), an instance of this class can be turned into an array (including all its embedded objects) which can then be further transformed easily into other formats, such as JSON, XML.

The methods fields() and extraFields() allow the implementing classes to customize how and which of their data should be formatted and put into the result of toArray().

公共方法

隐藏继承的方法

方法描述被定义在
extraFields() Returns the list of additional fields that can be returned by toArray() in addition to those listed in fields(). yii\base\Arrayable
fields() Returns the list of fields that should be returned by default by toArray() when no specific fields are specified. yii\base\Arrayable
toArray() Converts the object into an array. yii\base\Arrayable

方法详情

extraFields() 公共 抽象 方法

Returns the list of additional fields that can be returned by toArray() in addition to those listed in fields().

This method is similar to fields() except that the list of fields declared by this method are not returned by default by toArray(). Only when a field in the list is explicitly requested, will it be included in the result of toArray().

参见:

public abstract array extraFields()
return array

The list of expandable field names or field definitions. Please refer to fields() on the format of the return value.

fields() 公共 抽象 方法

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 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.
return [
    'email',
    'firstName' => 'first_name',
    'lastName' => 'last_name',
    'fullName' => function ($model) {
        return $model->first_name . ' ' . $model->last_name;
    },
];

参见 toArray().

public abstract array fields()
return array

The list of field names or field definitions.

toArray() 公共 抽象 方法

Converts the object into an array.

public abstract array toArray(array $fields = [], array $expand = [], $recursive true)
$fields array

The fields that the output array should contain. Fields not specified in fields() will be ignored. If this parameter is empty, all fields as specified in fields() will be returned.

$expand array

The additional fields that the output array should contain. Fields not specified in extraFields() will be ignored. If this parameter is empty, no extra fields will be returned.

$recursive boolean

Whether to recursively return array representation of embedded objects.

return array

The array representation of the object