Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dfc31bc

Browse files
authored
Merge pull request #6740 from mduplouy/DDC288
Fix operator when using criteria on ManyToMany Fixes: doctrine/common#600
2 parents dda42f6 + 7c28a93 commit dfc31bc

3 files changed

Lines changed: 117 additions & 8 deletions

File tree

lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,11 @@ public function loadCriteria(PersistentCollection $collection, Criteria $criteri
259259
$parameters = $this->expandCriteriaParameters($criteria);
260260

261261
foreach ($parameters as $parameter) {
262-
list($name, $value) = $parameter;
263-
$field = $this->quoteStrategy->getColumnName($name, $targetClass, $this->platform);
264-
$whereClauses[] = sprintf('te.%s = ?', $field);
265-
$params[] = $value;
262+
[$name, $value, $operator] = $parameter;
263+
264+
$field = $this->quoteStrategy->getColumnName($name, $targetClass, $this->platform);
265+
$whereClauses[] = sprintf('te.%s %s ?', $field, $operator);
266+
$params[] = $value;
266267
}
267268

268269
$tableName = $this->quoteStrategy->getTableName($targetClass, $this->platform);

lib/Doctrine/ORM/Persisters/SqlValueVisitor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ class SqlValueVisitor extends ExpressionVisitor
5050
*/
5151
public function walkComparison(Comparison $comparison)
5252
{
53-
$value = $this->getValueFromComparison($comparison);
54-
$field = $comparison->getField();
55-
$operator = $comparison->getOperator();
53+
$value = $this->getValueFromComparison($comparison);
54+
$field = $comparison->getField();
55+
$operator = $comparison->getOperator();
5656

5757
if (($operator === Comparison::EQ || $operator === Comparison::IS) && $value === null) {
5858
return;
@@ -61,7 +61,7 @@ public function walkComparison(Comparison $comparison)
6161
}
6262

6363
$this->values[] = $value;
64-
$this->types[] = [$field, $value];
64+
$this->types[] = [$field, $value, $operator];
6565
}
6666

6767
/**
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Doctrine\Tests\ORM\Functional\Ticket;
4+
5+
use Doctrine\Tests\OrmFunctionalTestCase;
6+
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
7+
use Doctrine\Tests\Models\ECommerce\ECommerceCategory;
8+
use Doctrine\Common\Collections\Criteria;
9+
10+
final class GH6740Test extends OrmFunctionalTestCase
11+
{
12+
/**
13+
* @var int
14+
*/
15+
private $productId;
16+
17+
/**
18+
* @var int
19+
*/
20+
private $firstCategoryId;
21+
22+
/**
23+
* @var int
24+
*/
25+
private $secondCategoryId;
26+
27+
public function setUp() : void
28+
{
29+
$this->useModelSet('ecommerce');
30+
31+
parent::setUp();
32+
33+
$product = new ECommerceProduct();
34+
$product->setName('First Product');
35+
36+
$firstCategory = new ECommerceCategory();
37+
$secondCategory = new ECommerceCategory();
38+
39+
$firstCategory->setName('Business');
40+
$secondCategory->setName('Home');
41+
42+
$product->addCategory($firstCategory);
43+
$product->addCategory($secondCategory);
44+
45+
$this->_em->persist($product);
46+
$this->_em->flush();
47+
$this->_em->clear();
48+
49+
$this->productId = $product->getId();
50+
$this->firstCategoryId = $firstCategory->getId();
51+
$this->secondCategoryId = $secondCategory->getId();
52+
}
53+
54+
/**
55+
* @group 6740
56+
*/
57+
public function testCollectionFilteringLteOperator() : void
58+
{
59+
$product = $this->_em->find(ECommerceProduct::class, $this->productId);
60+
$criteria = Criteria::create()->where(Criteria::expr()->lte('id', $this->secondCategoryId));
61+
62+
self::assertCount(2, $product->getCategories()->matching($criteria));
63+
}
64+
65+
/**
66+
* @group 6740
67+
*/
68+
public function testCollectionFilteringLtOperator() : void
69+
{
70+
$product = $this->_em->find(ECommerceProduct::class, $this->productId);
71+
$criteria = Criteria::create()->where(Criteria::expr()->lt('id', $this->secondCategoryId));
72+
73+
self::assertCount(1, $product->getCategories()->matching($criteria));
74+
}
75+
76+
/**
77+
* @group 6740
78+
*/
79+
public function testCollectionFilteringGteOperator() : void
80+
{
81+
$product = $this->_em->find(ECommerceProduct::class, $this->productId);
82+
$criteria = Criteria::create()->where(Criteria::expr()->gte('id', $this->firstCategoryId));
83+
84+
self::assertCount(2, $product->getCategories()->matching($criteria));
85+
}
86+
87+
/**
88+
* @group 6740
89+
*/
90+
public function testCollectionFilteringGtOperator() : void
91+
{
92+
$product = $this->_em->find(ECommerceProduct::class, $this->productId);
93+
$criteria = Criteria::create()->where(Criteria::expr()->gt('id', $this->firstCategoryId));
94+
95+
self::assertCount(1, $product->getCategories()->matching($criteria));
96+
}
97+
98+
/**
99+
* @group 6740
100+
*/
101+
public function testCollectionFilteringEqualsOperator() : void
102+
{
103+
$product = $this->_em->find(ECommerceProduct::class, $this->productId);
104+
$criteria = Criteria::create()->where(Criteria::expr()->eq('id', $this->firstCategoryId));
105+
106+
self::assertCount(1, $product->getCategories()->matching($criteria));
107+
}
108+
}

0 commit comments

Comments
 (0)