src/PN/Bundle/ProductBundle/Controller/Administration/OccasionController.php
/**
     * add or update product tp occasion.
     *
     * @Route("/update-product/{id}", requirements={"id" = "\d+"}, name="occasion_manage_product_update_ajax", methods={"POST"})
     */
add or update product tp occasion.
add or update product tp occasion .
public function addOrUpdateProductToOccasionAction(Request $request, Occasion $occasion)
    {
        $this->denyAccessUnlessGranted(['ROLE_ADMIN', 'ROLE_MANAGE_PRODUCTS']);
        $productId = $request->request->get('id');
        $em = $this->getDoctrine()->getManager();
        if (!is_numeric($productId)) {
            $type = $request->request->get('type');
            $productIds = json_decode($productId);
            $products = $em->getRepository(Product::class)->findBy(["id" => $productIds]);
            foreach ($products as $product) {
                $checkOccasionHasProduct = $em->getRepository(ProductHasOccasion::class)->findOneBy([
                    "product" => $product,
                    "occasion" => $occasion,
                ]);
                if (isset($type) AND $type == 'add') {
                    if (!$checkOccasionHasProduct) {
                        $this->addProductToOccasion($product, $occasion);
                    }
                } else {
                    if ($checkOccasionHasProduct) {
                        $em->remove($checkOccasionHasProduct);
                    }
                }
            }
            $return = 1;
        } else {
            $product = $em->getRepository(Product::class)->find($productId);
            $return = $this->addOrDeleteProductInOccasion($product, $occasion);
        }
        $em->flush();
        return $this->json(['value' => $return]);
    }
public function addOrUpdateProductToOccasionAction ( Request $ request , Occasion $ occasion ) { $ this -> denyAccessUnlessGranted ( [ 'ROLE_ADMIN' , 'ROLE_MANAGE_PRODUCTS' ] ) ; $ productId = $ request -> request -> get ( 'id' ) ; $ em = $ this -> getDoctrine ( ) -> getManager ( ) ; if ( ! is_numeric ( $ productId ) ) { $ type = $ request -> request -> get ( 'type' ) ; $ productIds = json_decode ( $ productId ) ; $ products = $ em -> getRepository ( Product :: class ) -> findBy ( [ " id " => $ productIds ] ) ; foreach ( $ products as $ product ) { $ checkOccasionHasProduct = $ em -> getRepository ( ProductHasOccasion :: class ) -> findOneBy ( [ " product " => $ product , " occasion " => $ occasion , ] ) ; if ( isset ( $ type ) AND $ type == 'add' ) { if ( ! $ checkOccasionHasProduct ) { $ this -> addProductToOccasion ( $ product , $ occasion ) ; } } else { if ( $ checkOccasionHasProduct ) { $ em -> remove ( $ checkOccasionHasProduct ) ; } } } $ return = 1 ; } else { $ product = $ em -> getRepository ( Product :: class ) -> find ( $ productId ) ; $ return = $ this -> addOrDeleteProductInOccasion ( $ product , $ occasion ) ; } $ em -> flush ( ) ; return $ this -> json ( [ 'value' => $ return ] ) ; }
add or update product tp occasion.
add or update product tp occasion .