Пример #1
0
 /**
  * Set product categories and main category
  * @param array $categories ids.
  * @param integer $main_category Main category id.
  */
 public function setCategories(array $categories, $main_category)
 {
     $dontDelete = array();
     if (!StoreCategory::model()->countByAttributes(array('id' => $main_category))) {
         $main_category = 1;
     }
     if (!in_array($main_category, $categories)) {
         array_push($categories, $main_category);
     }
     foreach ($categories as $c) {
         $count = StoreProductCategoryRef::model()->countByAttributes(array('category' => $c, 'product' => $this->id));
         if ($count == 0) {
             $record = new StoreProductCategoryRef();
             $record->category = (int) $c;
             $record->product = $this->id;
             $record->save(false);
         }
         $dontDelete[] = $c;
     }
     // Clear main category
     StoreProductCategoryRef::model()->updateAll(array('is_main' => 0), 'product=:p', array(':p' => $this->id));
     // Set main category
     StoreProductCategoryRef::model()->updateAll(array('is_main' => 1), 'product=:p AND category=:c ', array(':p' => $this->id, ':c' => $main_category));
     // Delete not used relations
     if (sizeof($dontDelete) > 0) {
         $cr = new CDbCriteria();
         $cr->addNotInCondition('category', $dontDelete);
         StoreProductCategoryRef::model()->deleteAllByAttributes(array('product' => $this->id), $cr);
     } else {
         // Delete all relations
         StoreProductCategoryRef::model()->deleteAllByAttributes(array('product' => $this->id));
     }
 }
Пример #2
0
 public function afterDelete()
 {
     //Remove all products with this category set as main.
     $products = StoreProductCategoryRef::model()->findAllByAttributes(array('category' => $this->id, 'is_main' => '1'));
     foreach ($products as $p) {
         $productModel = StoreProduct::model()->findByPk($p->product);
         if ($productModel) {
             $productModel->delete();
         }
     }
     // Remove all category-product relations
     StoreProductCategoryRef::model()->deleteAllByAttributes(array('category' => $this->id, 'is_main' => '0'));
     $this->clearRouteCache();
     return parent::afterDelete();
 }