Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to configure hints for the QueryBuilder #6359

Closed
wants to merge 1 commit into from

Conversation

sstok
Copy link

@sstok sstok commented Mar 26, 2017

Background: In the RollerworksSearch (Doctrine ORM ConditionGenerator) I use a Query-Hint to pass conversion information to DQL functions. But now to integrate RollerworksSearch with the Api-platform I need to work with the QueryBuilder which doesn't allow to set hints.

I copied the description from the AbstractQuery class. If possible can this be back ported to 2.5 also? I know you prefer to merge everything to master but without this I'm kinda stuck, and I don't know when 2.6 will be released 😅

@sstok
Copy link
Author

sstok commented Mar 26, 2017

Travis failures seem unrelated 😃

@sstok sstok force-pushed the feature/query-builder-hints branch from a7f3d50 to 7fb926c Compare March 26, 2017 10:06
@sstok sstok force-pushed the feature/query-builder-hints branch from 7fb926c to d440e7c Compare March 27, 2017 07:19
@lcobucci
Copy link
Member

But what's wrong with $queryBuilder->getQuery()->setHint();? I don't think it's really necessary to support this...

@sstok
Copy link
Author

sstok commented Mar 27, 2017

The Query is generated after the building process, so I don't have any control over it.

https://github.com/api-platform/core/blob/master/src/Bridge/Doctrine/Orm/CollectionDataProvider.php#L65-L70

In the case an extension is able to provide the data, return $queryBuilder->getQuery()->getResult(); is never executed and the Extension (which can be any) will generate the Query and use the result in a wrapped object (like a paginator).

I set the Query hint as follow:

    public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
    {
        $request = $this->requestStack->getCurrentRequest();

        /** @var SearchCondition $condition */
        if (!$request || null === $condition = $request->attributes->get('_api_search_condition')) {
            return;
        }

        if (!method_exists($queryBuilder, 'setHint')) {
            return;
        }

        $fieldSetName = $condition->getFieldSet();
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);

        if (null !== $attributes = $resourceMetadata->getAttribute('rollerworks_search')) {
            return;
        }

        if (empty($attributes['doctrine_orm']['*']) && empty($attributes['doctrine_orm'][$fieldSetName])) {
            return;
        }

        if (isset($resourceMetadata['accepted_fieldsets'])) {
            $condition->assertFieldSetName(...array_push($resourceMetadata['accepted_fieldsets'], $resourceClass));
        }

        $fieldSetName = $condition->getFieldSet();
        $setName = isset($attributes['doctrine_orm'][$fieldSetName]) ? $fieldSetName : '*';
        $configuration = $attributes['doctrine_orm'][$setName];

        $this->configureRelations($resourceClass, $configuration, $setName, $queryBuilder);

        $query = $queryBuilder->getEntityManager()->createQuery(); // Dummy to get the EntityManager
        $conditionGenerator = $this->ormFactory->createCachedConditionGenerator($this->ormFactory->createConditionGenerator($query, $condition));

        $this->configureMappings($resourceClass, $configuration, $setName, $conditionGenerator);

        $queryBuilder->andWhere($conditionGenerator->getWhereClause());
        $queryBuilder->setHint($conditionGenerator->getQueryHintName(), $conditionGenerator->getQueryHintValue()); // This cannot be done afterwards, 
    }

sstok added a commit to rollerworks/search-doctrine-orm that referenced this pull request Mar 28, 2017
This PR was merged into the 2.0-dev branch.

Discussion
----------

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | 
| License       | MIT
| Doc PR        | 

This allow QueryBuilder to be used when the required method exists ( doctrine/orm#6359 ).

For now it's an undocumented feature, mainly for the Apl-Platform integration (testing is near impossible without this).

Commits
-------

78aa9e0 Allow QueryBuilder when hints are supported
@sstok
Copy link
Author

sstok commented Sep 13, 2020

Notice. I no longer need this feature as it's solved different in rollerworks/search#286

Feel free to close this 👍

@greg0ire greg0ire closed this Sep 13, 2020
@sstok sstok deleted the feature/query-builder-hints branch September 14, 2020 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants