Interface yii\base\Arrayable

Implemented byluya\admin\file\Item, luya\admin\folder\Item, luya\admin\image\Item, luya\admin\models\ApiUser, luya\admin\models\Auth, luya\admin\models\Config, luya\admin\models\Group, luya\admin\models\GroupAuth, luya\admin\models\Lang, luya\admin\models\Logger, luya\admin\models\LoginForm, luya\admin\models\NgrestLog, luya\admin\models\Property, luya\admin\models\ProxyBuild, luya\admin\models\ProxyMachine, luya\admin\models\QueueLog, luya\admin\models\QueueLogError, luya\admin\models\ResetPasswordChangeForm, luya\admin\models\ResetPasswordForm, luya\admin\models\Scheduler, luya\admin\models\SearchData, luya\admin\models\StorageEffect, luya\admin\models\StorageFile, luya\admin\models\StorageFilter, luya\admin\models\StorageFilterChain, luya\admin\models\StorageFolder, luya\admin\models\StorageImage, luya\admin\models\Tag, luya\admin\models\TagRelation, luya\admin\models\User, luya\admin\models\UserAuthNotification, luya\admin\models\UserChangePassword, luya\admin\models\UserDevice, luya\admin\models\UserGroup, luya\admin\models\UserLogin, luya\admin\models\UserLoginLockout, luya\admin\models\UserOnline, luya\admin\models\UserRequest, luya\admin\ngrest\base\NgRestModel, luya\admin\storage\ItemAbstract, luya\base\DynamicModel, luya\cms\base\NavItemType, luya\cms\menu\Item, luya\cms\models\Block, luya\cms\models\BlockGroup, luya\cms\models\Config, luya\cms\models\Layout, luya\cms\models\Log, luya\cms\models\Nav, luya\cms\models\NavContainer, luya\cms\models\NavItem, luya\cms\models\NavItemModule, luya\cms\models\NavItemPage, luya\cms\models\NavItemPageBlockItem, luya\cms\models\NavItemRedirect, luya\cms\models\NavPermission, luya\cms\models\Property, luya\cms\models\Redirect, luya\cms\models\Theme, luya\cms\models\Website, luya\theme\ThemeConfig, luya\web\BaseLink, luya\web\EmailLink, luya\web\TelephoneLink, luya\web\WebsiteLink, luya\web\jsonld\AggregateRating, luya\web\jsonld\Article, luya\web\jsonld\BarOrPub, luya\web\jsonld\BaseThing, luya\web\jsonld\BlogPosting, luya\web\jsonld\CafeOrCoffeeShop, luya\web\jsonld\Comment, luya\web\jsonld\ContactPoint, luya\web\jsonld\Country, luya\web\jsonld\CreativeWork, luya\web\jsonld\EntertainmentBusiness, luya\web\jsonld\Event, luya\web\jsonld\FoodEstablishment, luya\web\jsonld\GeoCoordinates, luya\web\jsonld\ImageObject, luya\web\jsonld\LiveBlogPosting, luya\web\jsonld\LocalBusiness, luya\web\jsonld\MediaObject, luya\web\jsonld\NightClub, luya\web\jsonld\Offer, luya\web\jsonld\Organization, luya\web\jsonld\Person, luya\web\jsonld\Place, luya\web\jsonld\PostalAddress, luya\web\jsonld\PropertyValue, luya\web\jsonld\Rating, luya\web\jsonld\Restaurant, luya\web\jsonld\Review, luya\web\jsonld\SocialMediaPosting, luya\web\jsonld\Thing, yii\base\DynamicModel, yii\base\Model, yii\data\ActiveDataFilter, yii\data\DataFilter, yii\db\ActiveRecord, yii\db\BaseActiveRecord
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/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().

Public Methods

Hide inherited methods

Method Description Defined By
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

Method Details

Hide inherited methods

extraFields() public abstract method

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().

See also:

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.

                public function extraFields();

            
fields() public abstract 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 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;
    },
];

See also toArray().

public abstract array fields ( )
return array

The list of field names or field definitions.

                public function fields();

            
toArray() public abstract method

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

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