/**
  * @inheritdoc
  */
 public function save()
 {
     $dynamicModel = $this->attributeHandler->owner;
     /** @var ActiveRecord $valueClass */
     $valueClass = $dynamicModel->valueClass;
     $baseQuery = $valueClass::find()->where(['entityId' => $dynamicModel->entityModel->getPrimaryKey(), 'attributeId' => $this->attributeHandler->attributeModel->getPrimaryKey()]);
     $allOptions = [];
     foreach ($this->attributeHandler->attributeModel->options as $option) {
         $allOptions[] = $option->getPrimaryKey();
     }
     $query = clone $baseQuery;
     $query->andWhere("optionId NOT IN (:options)");
     $valueClass::deleteAll($query->where, ['options' => implode(',', $allOptions)]);
     // then we delete unselected options
     $selectedOptions = $dynamicModel->attributes[$this->attributeHandler->getAttributeName()];
     if (!is_array($selectedOptions)) {
         $selectedOptions = [];
     }
     $deleteOptions = array_diff($allOptions, $selectedOptions);
     $query = clone $baseQuery;
     $query->andWhere("optionId IN (:options)");
     $valueClass::deleteAll($query->where, ['options' => implode(',', $deleteOptions)]);
     // third we insert missing options
     foreach ($selectedOptions as $id) {
         $query = clone $baseQuery;
         $query->andWhere(['optionId' => $id]);
         /** @var AttributeValue $valueModel */
         $valueModel = $query->one();
         if (!$valueModel instanceof ActiveRecord) {
             $valueModel = new $valueClass();
             $valueModel->entityId = $dynamicModel->entityModel->getPrimaryKey();
             $valueModel->attributeId = $this->attributeHandler->attributeModel->getPrimaryKey();
             $valueModel->optionId = $id;
             if (!$valueModel->save()) {
                 throw new \Exception("Can't save value model");
             }
         }
     }
 }
Example #2
0
 /**
  * Constructor for creating form model from entity object
  *
  * @param array $params
  * @return static
  */
 public static function create($params)
 {
     $params['class'] = static::className();
     /** @var self $model */
     $model = Yii::createObject($params);
     foreach ($model->entityModel->getRelation($model->behavior->relationName)->all() as $attribute) {
         $handler = AttributeHandler::load($model, $attribute);
         $key = $handler->getAttributeName();
         $model->defineAttribute($key, $handler->valueHandler->load());
         $model->defineAttributeLabel($key, $attribute->getAttribute('name'));
         if ($attribute->required) {
             $model->addRule($key, 'required');
         }
         if ($handler->valueHandler instanceof RawValueHandler) {
             $model->addRule($key, 'default', ['value' => $attribute->defaultValue]);
         }
         if ($handler->valueHandler instanceof OptionValueHandler) {
             $model->addRule($key, 'default', ['value' => $attribute->defaultOptionId]);
         }
         $model->handlers[$key] = $handler;
     }
     return $model;
 }
Example #3
0
 public function init()
 {
     parent::init();
     $this->owner->addRule($this->getAttributeName(), 'string', ['max' => 255]);
 }
Example #4
0
 public function init()
 {
     parent::init();
     $this->owner->addRule($this->getAttributeName(), 'in', ['range' => $this->getOptions()]);
 }