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

Support for Array parameters in SQL filters #8375

Merged
merged 11 commits into from
Apr 19, 2021
Merged

Conversation

beberlei
Copy link
Member

@beberlei beberlei commented Dec 6, 2020

Supersedes #5571 by @mnogales

Fixes #2624

The original PR had a conceptual problem by putting the array detection directly into getParameter(), but this could be a BC break for array DBAL types and it could be a risk, because the generated SQL must be written by the filter implementor, so magically returning a quoted list instead a quoted value could break things.

Instead you must combine your generated SQL IN query with $this->getParameterList($name):

class CMSCountryFilter extends SQLFilter
{
    public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
    {
        if ($targetEntity->name !== CmsAddress::class || !$this->hasParameter('country')) {
            return "";
        }

        return $targetTableAlias.'.country IN (' . $this->getParameterList('country') . ')';
    }
}

To use getParameterList you must also pass the values using the new setParameterList method. It does not support type inference as we don't need to inspect the array elements then (which could be different).

$em->getFilters()->getFilter('country')->setParameter('country', ['de', 'es']);

Passing a list/single value and retrieving a single/list value (mixed up) will throw a new FilterException.

@beberlei beberlei changed the base branch from 2.8.x to 2.9.x February 28, 2021 22:19
Copy link
Member

@SenseException SenseException left a comment

Choose a reason for hiding this comment

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

Please also cover the two exception cases in getParameterList().

}

$param = $this->parameters[$name];
$isTraversable = is_array($param['value']); // WARNING: no Traversable support, because it might break on hashing
Copy link
Member

Choose a reason for hiding this comment

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

// WARNING: no Traversable support

Still, the variable is named $isTraversable :-D.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed traversable support explicitly now.

tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php Outdated Show resolved Hide resolved
@@ -401,7 +431,7 @@ public function testQueryGenerationDependsOnFilters(): void
$conf = $this->_em->getConfiguration();
$conf->addFilter('country', '\Doctrine\Tests\ORM\Functional\CMSCountryFilter');
$this->_em->getFilters()->enable('country')
->setParameter('country', 'en', DBALType::STRING);
->setParameter('country', ['en'], DBALType::STRING);
Copy link
Member

Choose a reason for hiding this comment

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

Why changing an existing test and filter?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because we have two filters in the test, one for locale one for country. By changing one to be a list we maximize test coverage without adding more tests.

@beberlei beberlei requested a review from SenseException April 18, 2021 13:22
SenseException
SenseException previously approved these changes Apr 18, 2021
Copy link
Member

@SenseException SenseException left a comment

Choose a reason for hiding this comment

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

After the typos that @greg0ire mentioned are fixed, this is 👍

@beberlei beberlei merged commit ddfee26 into doctrine:2.9.x Apr 19, 2021
@beberlei beberlei deleted the DDC-1952 branch April 19, 2021 17:46
Sign up for free to 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.

DDC-1952: Add support for array parameters on the SQLFilter
4 participants