getCollectionElementByProperty example


    protected function getVariantMappingOfImage(Image $image, Detail $variant)
    {
        $parent = $image->getParent();

        foreach ($parent->getMappings() as $mapping) {
            $match = true;

            foreach ($mapping->getRules() as $rule) {
                $option = $this->getCollectionElementByProperty(
                    $variant->getConfiguratorOptions(),
                    'id',
                    $rule->getOption()->getId()
                );
                if (!$option) {
                    $match = false;
                    break;
                }
            }
            if ($match) {
                return $mapping;
            }
$set = $this->findEntityByConditions(
            Group::class,
            [
                ['name' => $numbers[0]],
            ]
        );

        if (!$set instanceof Group) {
            throw new NotFoundException(sprintf('Filter set by name code %s not found', $numbers[0]));
        }

        $group = $this->getCollectionElementByProperty(
            $set->getOptions(),
            'name',
            $numbers[1]
        );

        if (!$group instanceof Option) {
            throw new NotFoundException(sprintf('Filter group by name code %s not found', $numbers[1]));
        }

        return $group->getId();
    }

    
/** * @template TEntity of ModelEntity * * @param ArrayCollection<array-key, TEntity> $collection * @param array<string, mixed> $conditions * * @return TEntity|null */
    protected function getCollectionElementByProperties(Collection $collection, array $conditions)
    {
        foreach ($conditions as $property => $value) {
            $entity = $this->getCollectionElementByProperty(
                $collection,
                $property,
                $value
            );
            if ($entity) {
                return $entity;
            }
        }

        return null;
    }

    
foreach ($mapping->getRules() as $rule) {
                $option = $rule->getOption();
                $alias = 'option' . $option->getId();
                $builder->innerJoin('variants.configuratorOptions', $alias, 'WITH', $alias . '.id = :' . $alias)
                    ->setParameter($alias$option->getId());
            }

            /** @var Query<Detail> $query */
            $query = $builder->getQuery();
            foreach ($query->getResult() as $variant) {
                if (!$force
                    && $this->getCollectionElementByProperty($variant->getImages(), 'parent', $mapping->getImage())
                ) {
                    continue;
                }

                $image = $this->getVariantResource()->createVariantImage(
                    $mapping->getImage(),
                    $variant
                );

                $variant->getImages()->add($image);
            }
        }
Home | Imprint | This part of the site doesn't use cookies.