Attribute::setTypes PHP Method

setTypes() public method

public setTypes ( array $types ) : boolean
$types array
return boolean
    public function setTypes(array $types)
    {
        $transaction = Yii::app()->getDb()->beginTransaction();
        try {
            TypeAttribute::model()->deleteAll('attribute_id = :attribute', [':attribute' => $this->id]);
            foreach ($types as $type) {
                $attribute = new TypeAttribute();
                $attribute->setAttributes(['attribute_id' => $this->id, 'type_id' => (int) $type]);
                $attribute->save();
            }
            $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()]);
 }