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(); } } }
public function up() { $db = Yii::app()->db; Yii::app()->setImport(['application.modules.store.models.*']); $cats = $db->createCommand("SELECT c1.* FROM site_store_category c1 LEFT JOIN site_store_category c2 ON c1.id=c2.parent_id WHERE c2.id IS NULL")->queryAll(); foreach ($cats as $cat) { $Type = new Type(); $Type->name = $cat['slug']; $Type->save(); for ($i = 2; $i < 9; ++$i) { $TypeAttribute = new TypeAttribute(); $TypeAttribute->type_id = $Type->id; $TypeAttribute->attribute_id = $i; $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; } }
public function saveSpec($spec, $product_id, $category_id, $type_id) { $name = \yupe\helpers\YText::translit($spec['name']); if (!in_array($name, $this->attr_name)) { if ($category_id) { // добавим в характеристику id категории $name = $name . $category_id; } else { print_r(" Error: can`t find StoreCategory for {$spec['id_product']} \n"); return; } } $model = Attribute::model()->findByAttributes(['name' => $name]); if (!$model) { $model = new Attribute(); $model->group_id = 1; $model->name = $name; $model->title = $spec['name']; $model->type = Attribute::TYPE_DROPDOWN; if ($model->save()) { $option = new AttributeOption(); $option->attribute_id = $model->id; $option->position = 0; $option->value = $spec['value']; if (!$option->save()) { echo "\n"; echo "Error: save AttributeOption for {$name} \n"; echo "\n"; } $TypeAttribute = new TypeAttribute(); $TypeAttribute->type_id = $type_id; $TypeAttribute->attribute_id = $model->id; if (!$TypeAttribute->save()) { echo "\n"; echo "Error: save TypeAttribute for {$name} \n"; echo "\n"; } } else { echo "\n"; echo "Error: save Attribute for {$name} \n"; echo "\n"; return; } } else { $this->addOption($spec, $model); } // свяжем продукт со значением характеристики $option = AttributeOption::model()->findByAttributes(['attribute_id' => $model->id, 'value' => $spec['value']]); if ($option) { $id = $option->id; $name = $model->name; $command = \Yii::app()->db->createCommand("\n INSERT INTO `site_store_product_attribute_eav` (`product_id`, `attribute`, `value`) \n VALUES ({$product_id}, '{$name}', {$id})\n ")->execute(); } }