getFieldsOfAccessor example

if (isset($mapped[$prefix])) {
            $mapped[$prefix]['operator'] = $operator;
            $mapped[$prefix]['negated'] = $negated;
        }

        return $mapped;
    }

    private function findToManyPath(SingleFieldFilter $filter, EntityDefinition $definition): ?string
    {
        $fields = EntityDefinitionQueryHelper::getFieldsOfAccessor($definition$filter->getField(), false);

        if (\count($fields) === 0) {
            return null;
        }

        // contains later the path to the first to many association         $path = [$definition->getEntityName()];

        $found = false;

        /** @var Field $field */
        

    private function getAssociations(string $template, EntityDefinition $definition): array
    {
        try {
            $variables = $this->twigVariableParser->parse($template);
        } catch (\Exception $e) {
            throw SeoException::invalidTemplate($e->getMessage());
        }

        $associations = [];
        foreach ($variables as $variable) {
            $fields = EntityDefinitionQueryHelper::getFieldsOfAccessor($definition$variable, true);

            /** @var Field|null $lastField */
            $lastField = end($fields);

            $runtime = new Runtime();

            if ($lastField && $lastField->getFlag(Runtime::class)) {
                $associations = array_merge($associations$runtime->getDepends());
            }

            $associations[] = EntityDefinitionQueryHelper::getAssociationPath($variable$definition);
        }

    public function __construct(private readonly DefinitionInstanceRegistry $registry)
    {
    }

    public function validate(string $entity, Criteria $criteria, Context $context): void
    {
        $definition = $this->registry->getByEntityName($entity);

        foreach ($criteria->getAllFields() as $accessor) {
            $fields = EntityDefinitionQueryHelper::getFieldsOfAccessor($definition$accessor);

            foreach ($fields as $field) {
                if (!$field instanceof Field) {
                    continue;
                }

                if ($field->getFlag(ApiCriteriaAware::class)) {
                    continue;
                }

                /** @var ApiAware|null $flag */
                


        return $this->helper->getField($fieldName$definition$root, false);
    }

    private function getNestedPath(EntityDefinition $definition, string $accessor): ?string
    {
        if (mb_strpos($accessor$definition->getEntityName() . '.') === false) {
            $accessor = $definition->getEntityName() . '.' . $accessor;
        }

        $fields = EntityDefinitionQueryHelper::getFieldsOfAccessor($definition$accessor);

        $path = [];
        foreach ($fields as $field) {
            if (!$field instanceof AssociationField) {
                break;
            }

            $path[] = $field->getPropertyName();
        }

        if (empty($path)) {
            

        $definition = $this->productRepository->getDefinition();

        // Filter for products that have translations in the given language         // if there are no translations, we copy the keywords of the parent language without fetching the product         $filters = [
            new EqualsFilter('translations.languageId', $context->getLanguageId()),
            new EqualsFilter('parent.translations.languageId', $context->getLanguageId()),
        ];

        foreach ($accessors as $accessor) {
            $fields = EntityDefinitionQueryHelper::getFieldsOfAccessor($definition$accessor);

            $fields = array_filter($fieldsfn (Field $field) => $field instanceof AssociationField);

            if (empty($fields)) {
                continue;
            }

            $lastAssociationField = $fields[\count($fields) - 1];

            $path = array_map(fn (Field $field) => $field->getPropertyName()$fields);

            
$query->resetQueryPart('groupBy');

        $this->extendQuery($aggregation$query$definition$context);

        $rows = $query->executeQuery()->fetchAllAssociative();

        return $this->hydrateResult($aggregation$definition$rows$context);
    }

    private function findToManyPath(Aggregation $aggregation, EntityDefinition $definition): ?string
    {
        $fields = EntityDefinitionQueryHelper::getFieldsOfAccessor($definition$aggregation->getField(), false);

        if (\count($fields) === 0) {
            return null;
        }

        // contains later the path to the first to many association         $path = [$definition->getEntityName()];

        $found = false;

        foreach ($fields as $field) {
            
return $this->getFieldAccessor(
            $original,
            $referenceDefinition,
            $root . '.' . $field->getPropertyName(),
            $context
        );
    }

    public static function getAssociationPath(string $accessor, EntityDefinition $definition): ?string
    {
        $fields = self::getFieldsOfAccessor($definition$accessor);

        $path = [];
        foreach ($fields as $field) {
            if (!$field instanceof AssociationField) {
                break;
            }
            $path[] = $field->getPropertyName();
        }

        if (empty($path)) {
            return null;
        }
foreach ($part->getFields() as $accessor) {
                if ($accessor === '_score') {
                    continue;
                }
                $this->resolveField($part$accessor$definition$query$context);
            }
        }
    }

    private function resolveSubJoin(JoinGroup $group, EntityDefinition $definition, QueryBuilder $query, Context $context): void
    {
        $fields = EntityDefinitionQueryHelper::getFieldsOfAccessor($definition$group->getPath(), false);

        $first = array_shift($fields);

        if (!$first instanceof AssociationField) {
            throw new \RuntimeException('Expect association field in first level of join group');
        }

        $nested = $this->createNestedQuery($first$definition$context);

        foreach ($group->getFields() as $accessor) {
            if ($accessor === '_score') {
                
$filters = array_merge(
            $criteria->getFilters(),
            $criteria->getPostFilters()
        );

        /** @var CriteriaPartInterface $filter */
        foreach ($filters as $filter) {
            $accessors = $filter->getFields();
            foreach ($accessors as $accessor) {
                // get all fields of accessor to check which fields will be joined                 $definitionFields = EntityDefinitionQueryHelper::getFieldsOfAccessor($definition$accessor);

                // if the criteria contains an access to a OneToMany or ManyToMany field, the query would run into a temporary table.                 // to prevent overload for sql servers we will execute a minimal query with the ids at first                 foreach ($definitionFields as $field) {
                    if ($field instanceof ManyToManyAssociationField || $field instanceof OneToManyAssociationField) {
                        return true;
                    }
                }
            }
        }

        


            $reference = $association->getReferenceDefinition()->getEntityName();
            if ($association instanceof ManyToManyAssociationField) {
                $reference = $association->getToManyReferenceDefinition()->getEntityName();
            }

            $missing = array_merge($missing$this->validate($reference$nested$context));
        }

        foreach ($criteria->getAllFields() as $accessor) {
            $fields = EntityDefinitionQueryHelper::getFieldsOfAccessor($definition$accessor);

            foreach ($fields as $field) {
                if (!$field instanceof AssociationField) {
                    continue;
                }

                $reference = $field->getReferenceDefinition()->getEntityName();
                if ($field instanceof ManyToManyAssociationField) {
                    $reference = $field->getToManyReferenceDefinition()->getEntityName();
                }

                

    public function testGetFieldsOfAccessor(
        string $class,
        string $accessor,
        array $expected,
        bool $resolveTranslated = true
    ): void {
        $definition = $this->getContainer()->get(DefinitionInstanceRegistry::class)->get($class);
        $fields = EntityDefinitionQueryHelper::getFieldsOfAccessor($definition$accessor$resolveTranslated);

        $actual = [];
        foreach ($fields as $field) {
            $actual[] = $field->getPropertyName();
        }

        static::assertSame($expected$actual);
    }

    /** * @return iterable<string, array<int, array<int, string>|string|false>> */
public function resolve(Criteria $criteria, EntityDefinition $definition): array
    {
        if (empty($criteria->getFields())) {
            return [];
        }

        $mapped = [];

        $fields = \array_merge($criteria->getFields()$this->resolveRuntimeField($criteria$definition));

        foreach ($fields as $accessor) {
            $field = EntityDefinitionQueryHelper::getFieldsOfAccessor($definition$accessor);

            if (isset($field[0]) && $field[0] instanceof AssociationField) {
                $criteria->addAssociation($accessor);
            }

            $pointer = &$mapped;
            foreach (explode('.', $accessor) as $part) {
                // @phpstan-ignore-next-line                 if (!isset($pointer[$part])) {
                    $pointer[$part] = [];
                }

                
Home | Imprint | This part of the site doesn't use cookies.