Trait Yiisoft\ActiveRecord\Trait\ArrayableTrait
| Uses Traits | Yiisoft\Arrays\ArrayableTrait |
|---|
Trait to implement {@see ArrayableInterface} interface for ActiveRecord.
See also:
Public Methods
| Method | Description | Defined By |
|---|---|---|
| extraFields() | Returns the list of fields that can be expanded further and returned by {@see toArray()}. | Yiisoft\ActiveRecord\Trait\ArrayableTrait |
| fields() | Returns the list of fields that should be returned by default by {@see toArray()}. | Yiisoft\ActiveRecord\Trait\ArrayableTrait |
| propertyNames() | Yiisoft\ActiveRecord\Trait\ArrayableTrait | |
| relatedRecords() | Yiisoft\ActiveRecord\Trait\ArrayableTrait |
Method Details
Returns the list of fields that can be expanded further and returned by {@see toArray()}.
This method is similar to {@see \Yiisoft\ActiveRecord\Trait\fields()} except that the list of fields returned by this method are not returned by default by {@see \Yiisoft\ActiveRecord\Trait\toArray()}. Only when field names to be expanded are explicitly specified when calling {@see \Yiisoft\ActiveRecord\Trait\toArray()}, will their values be exported.
The default implementation returns the names of the relations defined in this ActiveRecord class indexed by
themselves.
You may override this method to return a list of expandable fields.
| public (Closure|string)[] extraFields ( ) | ||
| return | (Closure|string)[] |
The list of expandable field names or field definitions. Please refer to {@see \Yiisoft\ActiveRecord\Trait\fields()} on the format of the return value. |
|---|---|---|
public function extraFields(): array
{
$fields = array_keys($this->relatedRecords());
return array_combine($fields, $fields);
}
Returns the list of fields that should be returned by default by {@see toArray()}.
A field is a named element in the returned array by {@see \Yiisoft\ActiveRecord\Trait\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;firstNameandlastName: the field names arefirstNameandlastName, and their values are obtained from thefirst_nameandlast_nameproperties;fullName: the field name isfullName. Its value is obtained by concatenatingfirst_nameandlast_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 returns the names of the properties of this record indexed by themselves.
| public (Closure|string)[] fields ( ) | ||
| return | (Closure|string)[] |
The list of field names or field definitions. |
|---|---|---|
public function fields(): array
{
$fields = $this->propertyNames();
return array_combine($fields, $fields);
}
Signup or Login in order to comment.