Attribute::setMultipleValuesAttributes PHP Method

setMultipleValuesAttributes() public method

public setMultipleValuesAttributes ( array $attributes ) : boolean
$attributes array
return boolean
    public function setMultipleValuesAttributes(array $attributes)
    {
        if (!$this->isMultipleValues()) {
            return true;
        }
        $transaction = Yii::app()->getDb()->beginTransaction();
        try {
            foreach ($attributes as $attribute) {
                $model = new AttributeOption();
                $model->setAttributes(['attribute_id' => $this->id, 'value' => trim($attribute)]);
                if (false === $model->save()) {
                    throw new CDbException('Error save attribute...');
                }
            }
            $transaction->commit();
            return true;
        } catch (Exception $e) {
            $transaction->rollback();
            return false;
        }
    }

Usage Example

Esempio n. 1
0
 /**
  * Создает новую модель атрибута.
  * Если создание прошло успешно - перенаправляет на просмотр.
  *
  * @return void
  */
 public function actionCreate()
 {
     $model = new Attribute();
     if (($data = Yii::app()->getRequest()->getPost('Attribute')) !== null) {
         $model->setAttributes($data);
         if ($model->save() && $model->setTypes(Yii::app()->getRequest()->getPost('types', [])) && $model->setMultipleValuesAttributes(explode(PHP_EOL, $model->rawOptions))) {
             Yii::app()->getUser()->setFlash(yupe\widgets\YFlashMessages::SUCCESS_MESSAGE, Yii::t('StoreModule.store', 'Attribute created'));
             $this->redirect((array) Yii::app()->getRequest()->getPost('submit-type', ['create']));
         }
     }
     $this->render('create', ['model' => $model, 'types' => Type::model()->findAll()]);
 }