Add support for STARTS_WITH and ENDS_WITH comparison operators
This commit is contained in:
parent
38bfcc6a7a
commit
a557c97a93
@ -16,7 +16,7 @@
|
||||
"require": {
|
||||
"php": "^7.0",
|
||||
"ext-pdo": "*",
|
||||
"doctrine/collections": "~1.3",
|
||||
"doctrine/collections": "^1.4",
|
||||
"doctrine/dbal": ">=2.5-dev,<2.7-dev",
|
||||
"doctrine/instantiator": "~1.0.1",
|
||||
"doctrine/common": "^2.7.1",
|
||||
|
@ -716,6 +716,8 @@ methods:
|
||||
* ``in($field, array $values)``
|
||||
* ``notIn($field, array $values)``
|
||||
* ``contains($field, $value)``
|
||||
* ``startsWith($field, $value)``
|
||||
* ``endsWith($field, $value)``
|
||||
|
||||
|
||||
.. note::
|
||||
|
@ -97,6 +97,8 @@ class BasicEntityPersister implements EntityPersister
|
||||
Comparison::IN => 'IN (%s)',
|
||||
Comparison::NIN => 'NOT IN (%s)',
|
||||
Comparison::CONTAINS => 'LIKE %s',
|
||||
Comparison::STARTS_WITH => 'LIKE %s',
|
||||
Comparison::ENDS_WITH => 'LIKE %s',
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -111,8 +111,18 @@ class SqlValueVisitor extends ExpressionVisitor
|
||||
{
|
||||
$value = $comparison->getValue()->getValue();
|
||||
|
||||
return $comparison->getOperator() == Comparison::CONTAINS
|
||||
? "%{$value}%"
|
||||
: $value;
|
||||
switch ($comparison->getOperator()) {
|
||||
case Comparison::CONTAINS:
|
||||
return "%{$value}%";
|
||||
|
||||
case Comparison::STARTS_WITH:
|
||||
return "{$value}%";
|
||||
|
||||
case Comparison::ENDS_WITH:
|
||||
return "%{$value}";
|
||||
|
||||
default:
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -185,6 +185,16 @@ class QueryExpressionVisitor extends ExpressionVisitor
|
||||
$parameter->setValue('%' . $parameter->getValue() . '%', $parameter->getType());
|
||||
$this->parameters[] = $parameter;
|
||||
|
||||
return $this->expr->like($field, $placeholder);
|
||||
case Comparison::STARTS_WITH:
|
||||
$parameter->setValue($parameter->getValue() . '%', $parameter->getType());
|
||||
$this->parameters[] = $parameter;
|
||||
|
||||
return $this->expr->like($field, $placeholder);
|
||||
case Comparison::ENDS_WITH:
|
||||
$parameter->setValue('%' . $parameter->getValue(), $parameter->getType());
|
||||
$this->parameters[] = $parameter;
|
||||
|
||||
return $this->expr->like($field, $placeholder);
|
||||
default:
|
||||
$operator = self::convertComparisonOperator($comparison->getOperator());
|
||||
|
@ -910,6 +910,38 @@ class EntityRepositoryTest extends OrmFunctionalTestCase
|
||||
$this->assertEquals(2, count($users));
|
||||
}
|
||||
|
||||
public function testMatchingCriteriaStartsWithComparison()
|
||||
{
|
||||
$this->loadFixture();
|
||||
|
||||
$repository = $this->_em->getRepository(CmsUser::class);
|
||||
|
||||
$users = $repository->matching(new Criteria(Criteria::expr()->startsWith('name', 'Foo')));
|
||||
$this->assertCount(0, $users);
|
||||
|
||||
$users = $repository->matching(new Criteria(Criteria::expr()->startsWith('name', 'R')));
|
||||
$this->assertCount(1, $users);
|
||||
|
||||
$users = $repository->matching(new Criteria(Criteria::expr()->startsWith('status', 'de')));
|
||||
$this->assertCount(2, $users);
|
||||
}
|
||||
|
||||
public function testMatchingCriteriaEndsWithComparison()
|
||||
{
|
||||
$this->loadFixture();
|
||||
|
||||
$repository = $this->_em->getRepository(CmsUser::class);
|
||||
|
||||
$users = $repository->matching(new Criteria(Criteria::expr()->endsWith('name', 'foo')));
|
||||
$this->assertCount(0, $users);
|
||||
|
||||
$users = $repository->matching(new Criteria(Criteria::expr()->endsWith('name', 'oman')));
|
||||
$this->assertCount(1, $users);
|
||||
|
||||
$users = $repository->matching(new Criteria(Criteria::expr()->endsWith('status', 'ev')));
|
||||
$this->assertCount(2, $users);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group DDC-2478
|
||||
*/
|
||||
|
@ -68,6 +68,9 @@ class QueryExpressionVisitorTest extends TestCase
|
||||
|
||||
[$cb->contains('field', 'value'), $qb->like('o.field', ':field'), new Parameter('field', '%value%')],
|
||||
|
||||
[$cb->startsWith('field', 'value'), $qb->like('o.field', ':field'), new Parameter('field', 'value%')],
|
||||
[$cb->endsWith('field', 'value'), $qb->like('o.field', ':field'), new Parameter('field', '%value')],
|
||||
|
||||
// Test parameter conversion
|
||||
[$cb->eq('object.field', 'value'), $qb->eq('o.object.field', ':object_field'), new Parameter('object_field', 'value')],
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user