public function setData(shopProduct $product, $data) { $data = array_unique(array_map('intval', $data)); $key = array_search(0, $data, true); if ($key !== false) { unset($data[$key]); } $category_ids = array_keys($this->getByField('product_id', $product->id, 'category_id')); if ($obsolete = array_diff($category_ids, $data)) { $this->deleteByField(array('product_id' => $product->id, 'category_id' => $obsolete)); // correct counter $category_model = new shopCategoryModel(); $category_model->recount($obsolete); } if ($added = array_diff($data, $category_ids)) { $this->add($product->id, $added); } //$product_model = new shopProductModel(); //$product_model->correctMainCategory($product->id); if ($data) { $product->category_id = reset($data); } else { $product->category_id = null; } $product_model = new shopProductModel(); $product_model->updateById($product->id, array('category_id' => $product->category_id)); return $data; }
<?php $category_model = new shopCategoryModel(); $category_model->recount();