Skip to content

Instantly share code, notes, and snippets.

@marcuzy
Last active August 29, 2015 14:26
Show Gist options
  • Save marcuzy/d666c6725c41305a0faf to your computer and use it in GitHub Desktop.
Save marcuzy/d666c6725c41305a0faf to your computer and use it in GitHub Desktop.
Подмешивание геттеров с модели с лэйблами
<?php
trait DictGrouped
{
public function getGroupName()
{
return $this->dictGroup ? $this->dictGroup->name : '';
}
public function getBrandName()
{
return $this->brand ? $this->brand->name : '';
}
/**
* Как правило в моделях определен метод attributeLabels(), поэтому нельзя просто взять
* и безполезненно подмешать не только нужные геттеры/сеттеры, но и labels (как минимум, методы трэйта имеют меньший приорите
* и не перекрывают методы модели)Зато просто переопередлить функцию getAttributeLabel из BaseActiveRecord, здесь трейт
* может её переопределить.
**/
public function getAttributeLabel($attribute)
{
$labels = [
'groupName' => 'Группа',
'brandName'=>'Брэнд'
];
return isset($labels[$attribute]) ? $labels[$attribute] : parent::getAttributeLabel($attribute);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment