Type::storeTypeAttributes PHP Method

storeTypeAttributes() public method

public storeTypeAttributes ( array $attributes ) : boolean
$attributes array
return boolean
    public function storeTypeAttributes(array $attributes)
    {
        $transaction = Yii::app()->getDb()->beginTransaction();
        try {
            TypeAttribute::model()->deleteAllByAttributes(['type_id' => $this->id]);
            foreach ($attributes as $attributeId) {
                $typeAttribute = new TypeAttribute();
                $typeAttribute->type_id = $this->id;
                $typeAttribute->attribute_id = (int) $attributeId;
                $typeAttribute->save();
            }
            $transaction->commit();
            return true;
        } catch (Exception $e) {
            $transaction->rollback();
            return false;
        }
    }

Usage Example

 /**
  *
  */
 public function actionCreate()
 {
     $model = new Type();
     if (($data = Yii::app()->getRequest()->getPost('Type')) !== null) {
         $model->setAttributes($data);
         if ($model->save() && $model->storeTypeAttributes(Yii::app()->getRequest()->getPost('attributes', []))) {
             Yii::app()->getUser()->setFlash(yupe\widgets\YFlashMessages::SUCCESS_MESSAGE, Yii::t('StoreModule.store', 'Product type is created'));
             $this->redirect((array) Yii::app()->getRequest()->getPost('submit-type', ['create']));
         }
     }
     $this->render('create', ['model' => $model, 'availableAttributes' => Attribute::model()->findAll()]);
 }