0
\$\begingroup\$

I want improve performance of this code. One product or residence have multiple criterion wich have criterion have multiple criterion value.

public function createOrUpdateCriteriaIfEntityUpdated( HasCriterionInterface $ownerEntity, array $criteria, DateTime $dateImportStarted

) {
    if (!$ownerEntity->getIsUpdated()) {
        return false;
    }

    foreach ($criteria as $criterion) {
            $criterionValue = $this->criterionValueRepository->createOrUpdate($criterion, $dateImportStarted);
            $ownerEntity->addCriterionValue($criterionValue);
    }
    $this->em->persist($ownerEntity);

    return true;
}

Here i check criterion and create value if not eist but performance is very bad, for each ownerEntity n criteria with n criteria values. How i can improve performance?

public function createOrUpdate(
    Criterion $criterion,
    DateTime $dateImport
): CriterionValue {
    

    $comment = $criterion->getComment();
    $criterionDatabase = $this->getEntityManager()->getRepository(Criterion::class)
    ->findOneBy(['keyCriterion' => $criterion->getKeyCriterion()]);

    if(!$criterionDatabase) {
        $criterion->setUpdatedAt($dateImport);
        $criterionValue = $this->createCriterionValue($comment, $dateImport);
        $criterionValue->setCriterion($criterion);

        $this->getEntityManager()->persist($criterion);
        $this->getEntityManager()->persist($criterionValue);
        $this->getEntityManager()->flush();     

        return $criterionValue;
    } 

    $criterionDatabase->setLabelCriterion($criterion->getLabelCriterion());
    $criterionDatabase->setUpdatedAt($dateImport);

    $criterionValues = $criterionDatabase->getCriterionValues();

    $criterionValueMatch = null;
    foreach($criterionValues as $criterionValue) {
        if($comment === $criterionValue->getLabelCriterionValue()) {
            $criterionValueMatch = $criterionValue;
            break;
        }
    }

    if(!$criterionValueMatch){
        $criterionValue = $this->createCriterionValue($comment, $dateImport);
        $criterionValue->setCriterion($criterionDatabase);
        $this->getEntityManager()->persist($criterionValue);
        return $criterionValue;
    }
    
    return $criterionValueMatch;
}

private function createCriterionValue(
    string $comment,
    Datetime $dateImport
) : CriterionValue {
    return (new CriterionValue())->setLabelCriterionValue($comment)->setUpdatedAt($dateImport);
}

Thanks for you hep

\$\endgroup\$
1
  • \$\begingroup\$ The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How do I ask a good question?. \$\endgroup\$ – BCdotWEB Jan 13 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.