Skip to content

SearchFilter: Refactor code to reduce code duplication and makes it easier to add new strategies to classes inheriting from SearchFilter class - #3541

Merged
alanpoulain merged 2 commits into
api-platform:2.6from
jpierront:jpierront-patch-1
Mar 3, 2021

Conversation

@jpierront

@jpierrontjpierront commented May 1, 2020

Copy link
Copy Markdown
Contributor
QA
Bug fix?no
New feature?no
BC breaks?no
Deprecations?no
Tickets
LicenseMIT
Doc PRapi-platform/docs#...

All query manipulations are now located in the same method to ease the creation on classes inherited from SearchFilter.

Example:

class ExtendedSearchFilter extends SearchFilter
{
public const STRATEGY_OTHER = 'other';
protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuilder, string $alias, string $field, $fieldType, $values, bool $caseSensitive, string $valueParameter)
{
if (self::STRATEGY_OTHER === $strategy) {
// ...
return;
}
parent::addWhereByStrategy($strategy, $queryBuilder, $alias, $field, $fieldType, $values, $caseSensitive, $valueParameter);
}
}

We will be able to create easily a "not_int" strategy or more complexe cases.

@jpierront

Copy link
Copy Markdown
ContributorAuthor

I'm not sure of what I did for the MongoDbOdm part ;-)

@jpierront
jpierrontforce-pushed the jpierront-patch-1 branch 2 times, most recently from 5a3a193 to f117591CompareMay 1, 2020 21:10
@alanpoulain

Copy link
Copy Markdown
Member

Seems nice! Could you add a Behat test to validate its behavior? It will allow to make sure MongoDB works too.

* @throws InvalidArgumentException If strategy does not exist
*/
protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, $fieldType, $value, bool $caseSensitive)
protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuilder, string $alias, string $field, $fieldType, array $values, bool $caseSensitive, string $valueParameter)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a BC break.

@jpierrontjpierront changed the title SearchFilter: Allow strategy on associationsSearchFilter: Refactor code to reduce code duplication and makes it easier to add new strategies to classes inheriting from SearchFilter classMay 2, 2020
@jpierront

Copy link
Copy Markdown
ContributorAuthor

Finally, by creating the behat tests, I realized that my code was not really doing what I expected.

  • Doing a "partial" search on "relatedDumies.name" was already working before my PR.
  • Doing a "partial" search on "relatedDumies" (So on the identifier) is not accepted by Doctrine ORM (It doesn't accept to make a "LIKE" on identifier in this case)

Finally I’ll just keep the refactoring simplifying the creation of new strategies in the inherited classes. It was what I needed initially ;-)

@jpierront
jpierrontforce-pushed the jpierront-patch-1 branch 5 times, most recently from 7818e75 to d959323CompareMay 2, 2020 15:08
*
* @throws InvalidArgumentException If strategy does not exist
*/
protected function addEqualityMatchStrategy(string $strategy, Builder $aggregationBuilder, string $matchField, $fieldType, $values, bool $caseSensitive)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the class is final, it should be private.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we allow to extends the ORM version but not the ODM one ? :/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Because the ORM version will also be a final class in the next major version (3.0).

@jpierront

Copy link
Copy Markdown
ContributorAuthor

OK, so my original intention will lose its meaning in V3. ;-)
My refactoring still seems interesting to centralize the code related to the strategy in the same place.

Comment threadsrc/Bridge/Doctrine/MongoDbOdm/Filter/SearchFilter.php Outdated
Comment threadsrc/Bridge/Doctrine/MongoDbOdm/Filter/SearchFilter.php Outdated
* @throws InvalidArgumentException If strategy does not exist
*/
protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, $fieldType, $value, bool $caseSensitive)
protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, $fieldType, $values, bool $caseSensitive, string $valueParameter = null)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even like this, it's a BC break. You should mimic what has been done here: #3521.

Base automatically changed from master to mainJanuary 23, 2021 21:59
@alanpoulain
alanpoulain changed the base branch from main to 2.6March 3, 2021 09:59
…asier to add new strategies to classes inheriting from SearchFilter class
@alanpoulain
alanpoulain merged commit 9df5a5e into api-platform:2.6Mar 3, 2021
@alanpoulain

Copy link
Copy Markdown
Member

Thanks @jpierront.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@jpierront@alanpoulain