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);

    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) {
        $criterionValue = $this->createCriterionValue($comment, $dateImport);


        return $criterionValue;


    $criterionValues = $criterionDatabase->getCriterionValues();

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

        $criterionValue = $this->createCriterionValue($comment, $dateImport);
        return $criterionValue;
    return $criterionValueMatch;

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

Thanks for you hep

  • \$\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.