Cactiverecord::getrelatedattribute Proposition

Hello All,

We recently wrote a method for getting attributes through one or multiple active record relations. Maybe this would be something that could be considered to be added in some form in Yii2?


 * Returns an attribute for the given relation.

 * @param string|array $through the relation(s).

 * @param string $attribute the attribute name.

 * @param string $defaultValue the default value to return if the relation is not found.

 * @return string the result.


public function getRelatedAttribute($through,$attribute,$defaultValue='')





    foreach($through as $relation)






    return $model instanceof CActiveRecord && $model->hasAttribute($attribute)

            ? $model->getAttribute($attribute);

            : $defaultValue;


I don’t see the difference/advantage of that method compared with: $model->getRelated($relation)->$attribute

The difference is that you can use that method in e.g. views without checking if the relation actually exists and it won’t crash if the relation isn’t there.