Sonata\ProductBundle\Entity\ProductCategoryManager::addCategoryToProduct PHP Method

addCategoryToProduct() public method

public addCategoryToProduct ( Sonata\Component\Product\ProductInterface $product, Sonata\ClassificationBundle\Model\CategoryInterface $category, $main = false )
$product Sonata\Component\Product\ProductInterface
$category Sonata\ClassificationBundle\Model\CategoryInterface
    public function addCategoryToProduct(ProductInterface $product, CategoryInterface $category, $main = false)
    {
        if ($this->findOneBy(array('category' => $category, 'product' => $product))) {
            return;
        }
        //
        //        if (null !== $category->getType() && self::CATEGORY_PRODUCT_TYPE !== $category->getType()) {
        //            // Should we throw an exception instead?
        //            $category->setType(self::CATEGORY_PRODUCT_TYPE);
        //            $this->categoryManager->save($category);
        //        }
        $productCategory = $this->create();
        $productCategory->setProduct($product);
        $productCategory->setCategory($category);
        $productCategory->setEnabled(true);
        $productCategory->setMain($main);
        $product->addProductCategory($productCategory);
        $this->save($productCategory);
    }