public function setDefaultCategory($defaultCategoryId)
    {
        // Allow uncategorized products (NULL instead of 0, to bypass delete cascade constraint)
        if ($defaultCategoryId <= 0) {
            $defaultCategoryId = null;
        }

        /** @var ProductCategory $productCategory */
        $productCategory = ProductCategoryQuery::create()
            ->filterByProductId($this->getId())
            ->filterByDefaultCategory(true)
            ->findOne()
        ;

        if ($productCategory !== null && (int) $productCategory->getCategoryId() === (int) $defaultCategoryId) {
            return $this;
        }

        if ($productCategory !== null) {
            $productCategory->delete();
        }

        // checks if the product is already associated with the category and but not default
        if (null !== $productCategory = ProductCategoryQuery::create()->filterByProduct($this)->filterByCategoryId($defaultCategoryId)->findOne()) {
            $productCategory->setDefaultCategory(true)->save();
        } else {
            $position = (new ProductCategory())->setCategoryId($defaultCategoryId)->getNextPosition();

            (new ProductCategory())
                ->setProduct($this)
                ->setCategoryId($defaultCategoryId)
                ->setDefaultCategory(true)
                ->setPosition($position)
                ->save();

            $this->setPosition($position);
        }

        return $this;
    }
public function setDefaultCategory($defaultCategoryId)
    {
        // Allow uncategorized products (NULL instead of 0, to bypass delete cascade constraint)
        if ($defaultCategoryId <= 0) {
            $defaultCategoryId = null;
        }

        /** @var ProductCategory $productCategory */
        $productCategory = ProductCategoryQuery::create()
            ->filterByProductId($this->getId())
            ->filterByDefaultCategory(true)
            ->findOne()
        ;

        if ($productCategory !== null && (int) $productCategory->getCategoryId() === (int) $defaultCategoryId) {
            return $this;
        }

        if ($productCategory !== null) {
            $productCategory->delete();
        }

        // checks if the product is already associated with the category and but not default
        if (null !== $productCategory = ProductCategoryQuery::create()->filterByProduct($this)->filterByCategoryId($defaultCategoryId)->findOne()) {
            $productCategory->setDefaultCategory(true)->save();
        } else {
            $position = (new ProductCategory())->setCategoryId($defaultCategoryId)->getNextPosition();

            (new ProductCategory())
                ->setProduct($this)
                ->setCategoryId($defaultCategoryId)
                ->setDefaultCategory(true)
                ->setPosition($position)
                ->save();

            $this->setPosition($position);
        }

        return $this;
    }
public function setDefaultCategory ( $ defaultCategoryId ) { // Allow uncategorized products (NULL instead of 0, to bypass delete cascade constraint) if ( $ defaultCategoryId <= 0 ) { $ defaultCategoryId = null ; } /** @var ProductCategory $productCategory */ $ productCategory = ProductCategoryQuery :: create ( ) -> filterByProductId ( $ this -> getId ( ) ) -> filterByDefaultCategory ( true ) -> findOne ( ) ; if ( $ productCategory !== null && ( int ) $ productCategory -> getCategoryId ( ) === ( int ) $ defaultCategoryId ) { return $ this ; } if ( $ productCategory !== null ) { $ productCategory -> delete ( ) ; } // checks if the product is already associated with the category and but not default if ( null !== $ productCategory = ProductCategoryQuery :: create ( ) -> filterByProduct ( $ this ) -> filterByCategoryId ( $ defaultCategoryId ) -> findOne ( ) ) { $ productCategory -> setDefaultCategory ( true ) -> save ( ) ; } else { $ position = ( new ProductCategory ( ) ) -> setCategoryId ( $ defaultCategoryId ) -> getNextPosition ( ) ; ( new ProductCategory ( ) ) -> setProduct ( $ this ) -> setCategoryId ( $ defaultCategoryId ) -> setDefaultCategory ( true ) -> setPosition ( $ position ) -> save ( ) ; $ this -> setPosition ( $ position ) ; } return $ this ; }
Set default category for this product

@param int $defaultCategoryId the new default category id
@return $this
Set default category for this product