public function beforeSave($insert) {
parent::beforeSave($insert);
if(!$this->saveEavAttributes()){
return false;
}
return true;
}
public function events() {
return [
\yii\db\ActiveRecord::EVENT_BEFORE_INSERT => 'saveEavAttributes',
\yii\db\ActiveRecord::EVENT_BEFORE_UPDATE => 'saveEavAttributes',
];
}
public function saveEavAttributes() {
$noError = TRUE;
if (count($this->attributes > 0)) {
foreach ($this->attributes as $attribute) {
$attributeModel = $this->getAttributeModel($attribute);
if (empty($attribute->value)) {
if ($attribute->required) {
$attribute->addError($this->alias, Yii::t('yii', '{attribute} cannot be blank.', ['attribute' => $attribute->name]));
return FALSE;
} else {
$attributeModel->delete();
return;
}
}
$attributeModel->event_id = $this->owner->id;
$attributeModel->attribute_id = $attribute->id;
$attributeModel->value = $this->owner->eavAttributes->{$attribute->alias}->value;
if (!$attributeModel->save()) {
$attribute->addError($attribute->alias, str_replace('Value', $attribute->name, $attributeModel->errors['value'][0]));
$noError = FALSE;
}
}
}
return $noError;
}
<div class="form-group field-event-<?= $model->alias ?>">
<?= \yii\helpers\Html::label($model->name)?>
<?= \yii\helpers\Html::input('text', $attribute, $model->value, ['class' => 'form-control'])?>
<div class="has-error"><div class="help-block"><?= \yii\helpers\Html::error($model, $model->alias)?></div></div>
</div>
public function beforeSave($event)
{
$event->isValid = $this->saveEavAttributes();
}