Compare Duplicates Lines on uploaded Excel

I’m making a Symfony 5 website where users can import Excel Files to create products.
All products are links to a catalogue on CatalogueProduit Table, where I store Product, Catalogue and details from the products.
On some Excel Files some products can appear multiple times. I want to avoid this.
So I’ve made the following request on CatalogueProduitRepository :

    public function checkifExist($catalogueId, $produitId): ?CatalogueProduit {
        return $this->createQueryBuilder("catalogueProduit")
            ->leftJoin('catalogueProduit.catalogue', 'catalogue')
            ->leftJoin('catalogueProduit.produit', 'produit')
            ->andWhere('catalogueProduit.catalogue = :catalogueId')
            ->andWhere('catalogueProduit.produit = :produitId')
            ->setParameter('catalogueId', $catalogueId)
            ->setParameter('produitId', $produitId)
            ->getQuery()
            ->getOneOrNullResult();
    }

When I check from a Catalogue Id and product if something il already stored it works gret it return Null or the result.
But On my model where I create the products, I made the following code :


    private function checkProduitCatalogue($doctrine, $produit, $catalogue){
        $checkProduitCatalogue = $doctrine
        ->getRepository(CatalogueProduit::class)
            ->checkifExist($catalogue->getId(), $produit->getId());
        return $checkProduitCatalogue;
    }

    private function createCatalogueProduit($entityManager,$doctrine , $product, $catalogue):CatalogueProduit
    {
            $checkProductCatalogue = $this->checkProduitCatalogue($doctrine, $catalogue, $product);
            if ($checkProductCatalogue == null){
                $catalogueProduit = new CatalogueProduit();
                /**
Do stuff to create the productCatalogue
**/
                $entityManager->persist($catalogueProduit);
                $entityManager->flush();
                return $catalogueProduit;
            }


    }

And when i checked, I have some products duplicated. I don’t understand why it creates my products even with my check… Any idea about what’s wrong in my code ?

Thanks a lot

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *