entityManager = $entityManager; $this->pageAutoGeneRepository = $pageAutoGeneRepository; $this->pageRepository = $pageRepository; } public function addPageToGenerate(array $pageIds) { $pages = []; $pageRepo = $this->entityManager->getRepository(Page::class); $dupliRepo = $this->entityManager->getRepository(PageDuplicate::class); $pageOptionRepo = $this->entityManager->getRepository(PageOptions::class); foreach ($pageIds as $pageId) { $pageDupli = null; $pageGene = null; if (is_object($pageId)) { if ($pageId instanceof Page) { $page = $pageId; // Ensure that Page entity is attached on Doctrine's context if (!$this->entityManager->contains($page)) { $page = $this->entityManager->merge($page); } $pageGene = $this->pageAutoGeneRepository->findOneByPageId($page->getId()); } else if ($pageId instanceof PageDuplicate) { $pageDupli = $pageId; // Ensure that PageDuplicate entity is attached on Doctrine's context if (!$this->entityManager->contains($pageDupli)) { $pageDupli = $this->entityManager->merge($pageDupli); } $page = $pageId->getPage(); $pageGene = $this->pageAutoGeneRepository->findByPageAndDuplicate($page->getId(), $pageDupli->getId()); } else { throw new UnprocessableEntityMaestroException("Unhandled object " . get_class($pageId)); } } else { $page = $pageRepo->findOneBy(["id" => $pageId["id"]]); if (!empty($pageId["pageDuplicateId"])) { $pageDupli = $dupliRepo->findOneBy([ "id" => $pageId["pageDuplicateId"] ]); $pageGene = $this->pageAutoGeneRepository->findByPageAndDuplicate($pageId["id"], $pageId["pageDuplicateId"]); } else { $pageGene = $this->pageAutoGeneRepository->findOneByPageId($pageId["id"]); } } if (empty($pageGene)) { $option = $pageOptionRepo->find($page->getOption()->getId()); // If $option = $page->getOption(); is used in combo with entityManager->merge (without entityManager->merge doctrine persistence/cascade association is broken), the flush will only works for first $pageAutoGene $pageAutoGene = new PageAutoGene(); $pageAutoGene->setPageOption($option); $pageAutoGene->setPageDuplicate($pageDupli); $pages[] = $pageAutoGene; } } if (!empty($pages)) { $this->pageAutoGeneRepository->saveMultiple($pages); } return true; } public function findPageToGenerateByElementTypesAndElement(array $elementTypes, array $elements) { try { $pages = $this->pageRepository->findByElementTypeIds($elementTypes); if (!empty($pages)) { foreach ($pages as $page) { $pageProducts = json_decode($page->getIdProducts(), true); if (!empty($pageProducts)) { $pageProductIds = array_column($pageProducts, 'id'); $checkProduct = array_intersect($elements, $pageProductIds); if (!empty($checkProduct)) { $pageIds[] = ['id' => $page->getId()]; if (!$page->getPageDuplicates()->isEmpty()) { foreach ($page->getPageDuplicates() as $dupli) { $dupliProducts = json_decode($dupli->getIdProducts(), true); if (!empty($dupliProducts)) { $dupliProductsIds = array_column($dupliProducts, 'id'); $checkProductDupli = array_intersect($elements, $dupliProductsIds); if (!empty($checkProductDupli)) { $pageIds[] = ['id' => $page->getId(), "pageDuplicateId" => $dupli->getId()]; } } } } } } } if (!empty($pageIds)) { $this->addPageToGenerate($pageIds); } } return true; } catch (\Throwable $th) { return false; } } }