AttributeValue::store PHP Method

store() public method

public store ( $attributeId, $value, Product $product ) : boolean
$attributeId
$value
$product Product
return boolean
    public function store($attributeId, $value, Product $product)
    {
        $attribute = null;
        if (!isset($this->attributes[$attributeId])) {
            $attribute = Attribute::model()->findByPk($attributeId);
            $this->attributes[$attributeId] = $attribute;
        } else {
            $attribute = $this->attributes[$attributeId];
        }
        switch ($attribute->type) {
            case Attribute::TYPE_DROPDOWN:
                $this->option_value = empty($value) ? null : (int) $value;
                break;
            case Attribute::TYPE_CHECKBOX_LIST:
                $this->option_value = empty($value) ? null : (int) $value;
                break;
            case Attribute::TYPE_CHECKBOX:
                $this->number_value = empty($value) ? 0 : 1;
                break;
            case Attribute::TYPE_NUMBER:
                $this->number_value = $value === '' ? null : (double) $value;
                break;
            case Attribute::TYPE_TEXT:
                $this->text_value = $value;
                break;
            case Attribute::TYPE_SHORT_TEXT:
                $this->string_value = $value;
                break;
            default:
                throw new InvalidArgumentException('Error attribute!');
        }
        $this->product_id = $product->id;
        $this->attribute_id = $attribute->id;
        return $this->save();
    }

Usage Example

 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}}');
 }
All Usage Examples Of AttributeValue::store