getFieldAccessor example

$jsonPath = implode('.', $jsonPathParts);
        }

        $jsonValueExpr = sprintf(
            'JSON_EXTRACT(`%s`.`%s`, %s)',
            $root,
            $field->getStorageName(),
            (string) $this->connection->quote('$' . $jsonPath)
        );

        $embeddedField = $this->getField($jsonPath$field->getPropertyMapping());
        $accessor = $this->getFieldAccessor($jsonValueExpr$embeddedField);

        /* * Values extracted from json have distinct json types, that are different from normal value types. * We need to convert json nulls into sql nulls. * * For example: `JSON_EXTRACT('{"foo":null}', '$.foo') IS NOT NULL` */
        return sprintf('IF(JSON_TYPE(%s) != "NULL", %s, NULL)', $jsonValueExpr$accessor);
    }

    private function getField(string $path, array $fields): ?Field
    {


                // Only add manual _score sorting if the query contains a _score calculation and selection (i.e. the                 // criteria has a term or queries). Otherwise the SQL selection would fail because no _score field                 // exists in any entity.                 $query->addOrderBy('_score', $sorting->getDirection());
                $query->addState('_score');

                continue;
            }

            $accessor = $this->helper->getFieldAccessor($sorting->getField()$definition$definition->getEntityName()$context);

            if ($sorting instanceof CountSorting) {
                $query->addOrderBy(sprintf('COUNT(%s)', $accessor)$sorting->getDirection());

                continue;
            }

            if ($sorting->getNaturalSorting()) {
                $query->addOrderBy('LENGTH(' . $accessor . ')', $sorting->getDirection());
            }

            
\assert($aggregationStruct !== null, 'FilterAggregation always have an aggregation');

        $this->extendQuery($aggregationStruct$query$definition$context);
    }

    private function parseDateHistogramAggregation(
        DateHistogramAggregation $aggregation,
        QueryBuilder $query,
        EntityDefinition $definition,
        Context $context
    ): void {
        $accessor = $this->queryHelper->getFieldAccessor($aggregation->getField()$definition$definition->getEntityName()$context);

        if ($this->timeZoneSupportEnabled && $aggregation->getTimeZone()) {
            $accessor = 'CONVERT_TZ(' . $accessor . ', "UTC", "' . $aggregation->getTimeZone() . '")';
        }

        $groupBy = match ($aggregation->getInterval()) {
            DateHistogramAggregation::PER_MINUTE => 'DATE_FORMAT(' . $accessor . ', \'%Y-%m-%d %H:%i\')',
            DateHistogramAggregation::PER_HOUR => 'DATE_FORMAT(' . $accessor . ', \'%Y-%m-%d %H\')',
            DateHistogramAggregation::PER_DAY => 'DATE_FORMAT(' . $accessor . ', \'%Y-%m-%d\')',
            DateHistogramAggregation::PER_WEEK => 'DATE_FORMAT(' . $accessor . ', \'%Y-%v\')',
            DateHistogramAggregation::PER_MONTH => 'DATE_FORMAT(' . $accessor . ', \'%Y-%m\')',
            
string $root,
        Context $context
    ): ParseResult {
        $result = new ParseResult();

        /** @var ScoreQuery $query */
        foreach ($queries as $query) {
            $parsed = $this->parse($query->getQuery()$definition$context$root);

            foreach ($parsed->getWheres() as $where) {
                if ($query->getScoreField()) {
                    $field = $this->queryHelper->getFieldAccessor(
                        $query->getScoreField(),
                        $definition,
                        $root,
                        $context
                    );

                    $result->addWhere(
                        sprintf('IF(%s , %s * %s, 0)', $where(string) $this->connection->quote($query->getScore())$field)
                    );

                    continue;
                }


        if (!$field instanceof AssociationField) {
            throw new \RuntimeException(sprintf('Expected field "%s" to be instance of %s', $associationKey, AssociationField::class));
        }

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

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

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

        
$total->select(['COUNT(*)'])
            ->from(sprintf('(%s) total', $query->getSQL()))
            ->setParameters($query->getParameters()$query->getParameterTypes());

        return (int) $total->executeQuery()->fetchOne();
    }

    private function addGroupBy(EntityDefinition $definition, Criteria $criteria, Context $context, QueryBuilder $query, string $table): void
    {
        if ($criteria->getGroupFields()) {
            foreach ($criteria->getGroupFields() as $grouping) {
                $accessor = $this->queryHelper->getFieldAccessor($grouping->getField()$definition$definition->getEntityName()$context);

                $query->addGroupBy($accessor);
            }

            return;
        }

        if ($query->hasState(EntityDefinitionQueryHelper::HAS_TO_MANY_JOIN)) {
            $query->addGroupBy(
                EntityDefinitionQueryHelper::escape($table) . '.' . EntityDefinitionQueryHelper::escape('id')
            );
        }
Home | Imprint | This part of the site doesn't use cookies.