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='')
{
if(!is_array($through))
$through=array($through);
$model=$this;
foreach($through as $relation)
{
$model=$model->getRelated($relation);
if($model===null)
break;
}
return $model instanceof CActiveRecord && $model->hasAttribute($attribute)
? $model->getAttribute($attribute);
: $defaultValue;
}