public function setTypeAttributes($attributes) { TypeAttribute::model()->deleteAllByAttributes(['type_id' => $this->id]); if (is_array($attributes)) { foreach ($attributes as $attribute_id) { $typeAttribute = new TypeAttribute(); $typeAttribute->type_id = $this->id; $typeAttribute->attribute_id = $attribute_id; $typeAttribute->save(); } } }
/** * @param $attributes * @return bool */ 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; } }
/** * @param array $types * @return bool * @throws CDbException */ 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; } }