public function updateDefaultCategory($defaultCategoryId) { // Allow uncategorized products (NULL instead of 0, to bypass delete cascade constraint) if ($defaultCategoryId <= 0) { $defaultCategoryId = NULL; } // Update the default category $productCategory = ProductCategoryQuery::create()->filterByProductId($this->getId())->filterByDefaultCategory(true)->findOne(); if ($productCategory == null || $productCategory->getCategoryId() != $defaultCategoryId) { // Delete the old default category if ($productCategory !== null) { $productCategory->delete(); } // Add the new default category $productCategory = new ProductCategory(); $productCategory->setProduct($this)->setCategoryId($defaultCategoryId)->setDefaultCategory(true)->save(); } }
/** * @param Product $product The product object to add. */ protected function doAddProduct($product) { $productCategory = new ChildProductCategory(); $productCategory->setProduct($product); $this->addProductCategory($productCategory); // set the back reference to this object directly as using provided method either results // in endless loop or in multiple relations if (!$product->getCategories()->contains($this)) { $foreignCollection = $product->getCategories(); $foreignCollection[] = $this; } }
public function addCategory(ProductAddCategoryEvent $event) { if (ProductCategoryQuery::create()->filterByProduct($event->getProduct())->filterByCategoryId($event->getCategoryId())->count() <= 0) { $productCategory = new ProductCategory(); $productCategory->setProduct($event->getProduct())->setCategoryId($event->getCategoryId())->setDefaultCategory(false)->save(); } }