Skip to content

Commit

Permalink
Added final getHash method in SQLFilter to avoid caching the filtered…
Browse files Browse the repository at this point in the history
… queries.

Issue: doctrine#3955
  • Loading branch information
alecszaharia committed Nov 27, 2020
1 parent e8c4d7b commit 398ea44
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
21 changes: 21 additions & 0 deletions lib/Doctrine/ORM/Query/Filter/SQLFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,27 @@ final public function hasParameter($name)
return isset($this->parameters[$name]);
}

/**
* Return a unique hash base on filter class and his parametters
*
* @return string
*/
final public function getHash()
{
$parameterCount = count($this->parameters);
$hash = get_class($this).$parameterCount;
foreach ($this->parameters as $name => $param) {
if (is_object($param['value'])) {
$valueHash = spl_object_hash($param['value']);
} else {
$valueHash = $param['value'];
}
$hash .= $name.$param['type'].$valueHash;
}

return $hash;
}

/**
* Returns as string representation of the SQLFilter parameters (the state).
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Query/FilterCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public function getHash()
$filterHash = '';

foreach ($this->enabledFilters as $name => $filter) {
$filterHash .= $name . $filter;
$filterHash .= $name . $filter->getHash();
}

return $filterHash;
Expand Down
28 changes: 26 additions & 2 deletions tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,32 @@ public function testEntityManagerIsFilterEnabled() : void
self::assertFalse($em->getFilters()->isEnabled('foo_filter'));
}

public function testGetHash() : void
{
$em = $this->getEntityManager();
$this->configureFilters($em);

// Check the filter hash without parameters
$em->getFilters()->enable("locale");
$filter = $em->getFilters()->getFilter("locale");
self::assertEquals('Doctrine\Tests\ORM\Functional\MyLocaleFilter0',$filter->getHash());

// Check the filter hash with one parameters
$filter->setParameter('test','string');
self::assertEquals('Doctrine\Tests\ORM\Functional\MyLocaleFilter1test2string',$filter->getHash());

// Check the filter hash with two parameters
$filter->setParameter('test2','string2');
self::assertEquals('Doctrine\Tests\ORM\Functional\MyLocaleFilter2test2stringtest22string2',$filter->getHash());

// Check the filter hash with three parameters one being an object
$stdClass = new \stdClass();
$splHash = spl_object_hash($stdClass);
$filter->setParameter('test3', $stdClass);
self::assertEquals('Doctrine\Tests\ORM\Functional\MyLocaleFilter3test2stringtest22string2test32'.$splHash,$filter->getHash());
}


protected function configureFilters($em)
{
// Add filters to the configuration of the EM
Expand Down Expand Up @@ -248,9 +274,7 @@ public function testSQLFilterGetSetParameter() : void
->method('setFiltersStateDirty');

$filter = new MyLocaleFilter($em);

$filter->setParameter('locale', 'en', DBALType::STRING);

self::assertEquals("'en'", $filter->getParameter('locale'));
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/ORM/Query/FilterCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ public function testGetFilter() : void

self::assertInstanceOf(MyFilter::class, $filterCollection->getFilter(self::TEST_FILTER));
}

public function testGetHash()
{
$filterCollection = $this->em->getFilters();
$filterCollection->enable('testFilter');
$filterCollection->getFilter('testFilter');
$filterCollection->setParameter('test','string');
$hash = $filterCollection->getHash();

$this->assertEquals("testFilterDoctrine\Tests\ORM\Query\MyFilter1test2string",$hash);
}
}

class MyFilter extends SQLFilter
Expand Down

0 comments on commit 398ea44

Please sign in to comment.