Add support for STARTS_WITH and ENDS_WITH comparison operators
This commit is contained in:
parent
38bfcc6a7a
commit
a557c97a93
@ -16,7 +16,7 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"php": "^7.0",
|
"php": "^7.0",
|
||||||
"ext-pdo": "*",
|
"ext-pdo": "*",
|
||||||
"doctrine/collections": "~1.3",
|
"doctrine/collections": "^1.4",
|
||||||
"doctrine/dbal": ">=2.5-dev,<2.7-dev",
|
"doctrine/dbal": ">=2.5-dev,<2.7-dev",
|
||||||
"doctrine/instantiator": "~1.0.1",
|
"doctrine/instantiator": "~1.0.1",
|
||||||
"doctrine/common": "^2.7.1",
|
"doctrine/common": "^2.7.1",
|
||||||
|
@ -716,6 +716,8 @@ methods:
|
|||||||
* ``in($field, array $values)``
|
* ``in($field, array $values)``
|
||||||
* ``notIn($field, array $values)``
|
* ``notIn($field, array $values)``
|
||||||
* ``contains($field, $value)``
|
* ``contains($field, $value)``
|
||||||
|
* ``startsWith($field, $value)``
|
||||||
|
* ``endsWith($field, $value)``
|
||||||
|
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
@ -87,16 +87,18 @@ class BasicEntityPersister implements EntityPersister
|
|||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
static private $comparisonMap = [
|
static private $comparisonMap = [
|
||||||
Comparison::EQ => '= %s',
|
Comparison::EQ => '= %s',
|
||||||
Comparison::IS => '= %s',
|
Comparison::IS => '= %s',
|
||||||
Comparison::NEQ => '!= %s',
|
Comparison::NEQ => '!= %s',
|
||||||
Comparison::GT => '> %s',
|
Comparison::GT => '> %s',
|
||||||
Comparison::GTE => '>= %s',
|
Comparison::GTE => '>= %s',
|
||||||
Comparison::LT => '< %s',
|
Comparison::LT => '< %s',
|
||||||
Comparison::LTE => '<= %s',
|
Comparison::LTE => '<= %s',
|
||||||
Comparison::IN => 'IN (%s)',
|
Comparison::IN => 'IN (%s)',
|
||||||
Comparison::NIN => 'NOT IN (%s)',
|
Comparison::NIN => 'NOT IN (%s)',
|
||||||
Comparison::CONTAINS => 'LIKE %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();
|
$value = $comparison->getValue()->getValue();
|
||||||
|
|
||||||
return $comparison->getOperator() == Comparison::CONTAINS
|
switch ($comparison->getOperator()) {
|
||||||
? "%{$value}%"
|
case Comparison::CONTAINS:
|
||||||
: $value;
|
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());
|
$parameter->setValue('%' . $parameter->getValue() . '%', $parameter->getType());
|
||||||
$this->parameters[] = $parameter;
|
$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);
|
return $this->expr->like($field, $placeholder);
|
||||||
default:
|
default:
|
||||||
$operator = self::convertComparisonOperator($comparison->getOperator());
|
$operator = self::convertComparisonOperator($comparison->getOperator());
|
||||||
|
@ -910,6 +910,38 @@ class EntityRepositoryTest extends OrmFunctionalTestCase
|
|||||||
$this->assertEquals(2, count($users));
|
$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
|
* @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->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
|
// Test parameter conversion
|
||||||
[$cb->eq('object.field', 'value'), $qb->eq('o.object.field', ':object_field'), new Parameter('object_field', 'value')],
|
[$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