store() public method

public store ( $attributeId, $value, Product $product ) : boolean
$attributeId
$value
$product Product
return boolean
 public function safeUp()
 {
     $this->createTable('{{store_product_attribute_value}}', ['id' => 'pk', 'product_id' => 'INTEGER NOT NULL', 'attribute_id' => 'INTEGER NOT NULL', 'number_value' => 'REAL', 'string_value' => 'VARCHAR(250)', 'text_value' => 'TEXT', 'option_value' => 'INTEGER', 'create_time' => 'DATETIME'], $this->getOptions());
     //fk
     $this->addForeignKey('{{fk_product_attribute_product}}', '{{store_product_attribute_value}}', 'product_id', '{{store_product}}', 'id', 'CASCADE');
     $this->addForeignKey('{{fk_product_attribute_attribute}}', '{{store_product_attribute_value}}', 'attribute_id', '{{store_attribute}}', 'id', 'CASCADE');
     $this->addForeignKey('{{fk_product_attribute_option}}', '{{store_product_attribute_value}}', 'option_value', '{{store_attribute_option}}', 'id', 'CASCADE');
     //ix
     $this->createIndex('{{ix_product_attribute_number_value}}', '{{store_product_attribute_value}}', 'number_value');
     $this->createIndex('{{ix_product_attribute_string_value}}', '{{store_product_attribute_value}}', 'string_value');
     //перенести аттрибуты
     $attributes = Yii::app()->getDb()->createCommand('SELECT * FROM {{store_product_attribute_eav}}')->queryAll();
     $modelsAttr = [];
     foreach ($attributes as $attribute) {
         $product = Product::model()->findByPk($attribute['product_id']);
         if (null === $product) {
             continue;
         }
         if (!isset($modelsAttr[$attribute['attribute']])) {
             $model = Attribute::model()->find('name = :name', [':name' => $attribute['attribute']]);
             if (null === $model) {
                 continue;
             }
             $modelsAttr[$attribute['attribute']] = $model;
         }
         $value = new AttributeValue();
         $value->store($modelsAttr[$attribute['attribute']]->id, $attribute['value'], $product);
     }
     $this->dropTable('{{store_product_attribute_eav}}');
 }
Ejemplo n.º 2
0
 /**
  * @param array $attributes
  * @return bool
  */
 public function saveTypeAttributes(array $attributes)
 {
     $transaction = Yii::app()->getDb()->beginTransaction();
     try {
         foreach ($attributes as $attribute => $value) {
             if (null === $value) {
                 continue;
             }
             $model = AttributeValue::model()->find('product_id = :product AND attribute_id = :attribute', [':product' => $this->id, ':attribute' => $attribute]);
             //множественные значения
             if (is_array($value)) {
                 AttributeValue::model()->deleteAll('product_id = :product AND attribute_id = :attribute', [':product' => $this->id, ':attribute' => $attribute]);
                 foreach ($value as $val) {
                     $model = new AttributeValue();
                     if (false === $model->store($attribute, $val, $this)) {
                         throw new InvalidArgumentException('Error store attribute!');
                     }
                 }
             } else {
                 $model = $model ?: new AttributeValue();
                 if (false === $model->store($attribute, $value, $this)) {
                     throw new InvalidArgumentException('Error store attribute!');
                 }
             }
         }
         $transaction->commit();
         return true;
     } catch (Exception $e) {
         $transaction->rollback();
         return false;
     }
 }
Ejemplo n.º 3
0
 /**
  * @param array $attributes
  * @return bool
  */
 public function saveTypeAttributes(array $attributes)
 {
     $transaction = Yii::app()->getDb()->beginTransaction();
     try {
         AttributeValue::model()->deleteAll('product_id = :id', [':id' => $this->id]);
         foreach ($attributes as $attribute => $value) {
             if (null == $value) {
                 continue;
             }
             //необходимо определить в какое поле сохраняем значение
             $model = new AttributeValue();
             $model->store($attribute, $value, $this);
         }
         $transaction->commit();
     } catch (Exception $e) {
         $transaction->rollback();
         return false;
     }
 }