ProductForm::save PHP Method

save() public method

public save ( )
    public function save()
    {
        $product = Product::model()->findByPk($this->id);
        if (is_null($product)) {
            // insert
            // Product
            $product = new Product();
            $product->model = $this->model;
            $product->sku = $this->sku;
            $product->upc = $this->upc;
            $product->ean = $this->ean;
            $product->jan = $this->jan;
            $product->isbn = $this->isbn;
            $product->mpn = $this->mpn;
            $product->location = $this->location;
            $product->price = $this->price;
            $product->tax_class_id = $this->taxClass;
            $product->quantity = $this->quantity;
            $product->minimum = $this->minimumQuantity;
            $product->subtract = $this->subtractStock;
            $product->stock_status_id = $this->outOfStockStatus;
            $product->shipping = $this->requiresShipping;
            $product->image = $this->image;
            $product->date_available = $this->dateAvailable;
            $product->length = $this->dimensionL;
            $product->height = $this->dimensionH;
            $product->width = $this->dimensionW;
            $product->weight = $this->weight;
            $product->weight_class_id = $this->weightClass;
            $product->sort_order = $this->sortOrder;
            $product->status = $this->status;
            $product->manufacturer_id = $this->manufacturer;
            $product->save();
            // Description
            $description = new ProductDescription();
            $description->product_id = $product->product_id;
            $description->language_id = 1;
            // TODO: make this dynamic
            $description->name = $this->name;
            $description->meta_description = $this->metaTagDescription;
            $description->meta_keyword = $this->metaTagKeywords;
            $description->description = $this->description;
            $description->tag = $this->productTags;
            $description->save();
        } else {
            // update
            // Product
            $product->model = $this->model;
            $product->sku = $this->sku;
            $product->upc = $this->upc;
            $product->ean = $this->ean;
            $product->jan = $this->jan;
            $product->isbn = $this->isbn;
            $product->mpn = $this->mpn;
            $product->location = $this->location;
            $product->price = $this->price;
            $product->tax_class_id = $this->taxClass;
            $product->quantity = $this->quantity;
            $product->minimum = $this->minimumQuantity;
            $product->subtract = $this->subtractStock;
            $product->stock_status_id = $this->outOfStockStatus;
            $product->shipping = $this->requiresShipping;
            $product->image = $this->image;
            $product->date_available = $this->dateAvailable;
            $product->length = $this->dimensionL;
            $product->height = $this->dimensionH;
            $product->width = $this->dimensionW;
            $product->weight = $this->weight;
            $product->weight_class_id = $this->weightClass;
            $product->sort_order = $this->sortOrder;
            $product->status = $this->status;
            $product->manufacturer_id = $this->manufacturer;
            $product->save();
            // Description
            $product->description->name = $this->name;
            $product->description->meta_description = $this->metaTagDescription;
            $product->description->meta_keyword = $this->metaTagKeywords;
            $product->description->description = $this->description;
            $product->description->tag = $this->productTags;
            $product->description->save();
        }
        // SEO Keyword
        $product->updateSEOKeyword($this->seoKeyword);
        // Filters
        $product->clearAllFiltersRelations();
        if (isset($this->filters) && count($this->filters) > 0) {
            foreach ($this->filters as $filterId) {
                $product->addFilter($filterId);
            }
        }
        // Categories
        $product->clearAllCategoriesRelations();
        if (isset($this->categories) && count($this->categories)) {
            foreach ($this->categories as $categoryId) {
                $product->addToCategory($categoryId);
            }
        }
        // Stores
        $product->clearAllStoresRelations();
        if (isset($this->stores) && count($this->stores)) {
            foreach ($this->stores as $storeId) {
                $product->addToStore($storeId);
            }
        }
        // Downloads
        $product->clearAllDownloadsRelations();
        if (isset($this->downloads) && count($this->downloads)) {
            foreach ($this->downloads as $downloadId) {
                $product->addToDownload($downloadId);
            }
        }
        // Related Products
        $product->clearAllRelatedProductsRelations();
        if (isset($this->relatedProducts) && count($this->relatedProducts)) {
            foreach ($this->relatedProducts as $relatedId) {
                $product->addRelatedProduct($relatedId);
            }
        }
    }

Usage Example

Exemplo n.º 1
0
 public function actionUpdate($id)
 {
     $model = new ProductForm();
     if (isset($_POST['ProductForm'])) {
         $model->attributes = $_POST['ProductForm'];
         if ($model->validate()) {
             $model->save();
             $this->redirect(array('index'));
         }
     } else {
         $model->loadDataFromProduct($id);
     }
     $statuses = array(0 => Yii::t('common', 'Disabled'), 1 => Yii::t('common', 'Enabled'));
     $yes_no = array(0 => Yii::t('common', 'No'), 1 => Yii::t('common', 'Yes'));
     $taxClasses = TaxClass::model()->findAll();
     $taxClassesList = array();
     foreach ($taxClasses as $taxClass) {
         $taxClassesList[$taxClass->tax_class_id] = $taxClass->title;
     }
     // TODO: add language
     $stockStatuses = StockStatus::model()->findAll();
     $stockStatusesList = array();
     foreach ($stockStatuses as $stockStatus) {
         $stockStatusesList[$stockStatus->stock_status_id] = $stockStatus->name;
     }
     // TODO: add language
     $weightClasses = WeightClass::model()->findAll();
     $weightClassesList = array();
     foreach ($weightClasses as $weightClass) {
         $weightClassesList[$weightClass->weight_class_id] = $weightClass->description->title;
     }
     // TODO: add language
     $lengthClasses = LengthClass::model()->findAll();
     $lengthClassesList = array();
     foreach ($lengthClasses as $lengthClass) {
         $lengthClassesList[$lengthClass->length_class_id] = $lengthClass->description->title;
     }
     $this->render('update', array('model' => $model, 'statuses' => $statuses, 'taxClasses' => $taxClassesList, 'yes_no' => $yes_no, 'stockStatuses' => $stockStatusesList, 'weightClasses' => $weightClassesList, 'lengthClasses' => $lengthClassesList));
 }