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

(cherry picked from commit 398ea44)
  • Loading branch information
alecszaharia committed Apr 20, 2021
1 parent 10572ec commit 895172f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 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 @@ -123,6 +123,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 @@ -208,7 +208,7 @@ public function getHash()
$filterHash = '';

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

return $filterHash;
Expand Down
27 changes: 26 additions & 1 deletion tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,32 @@ public function testEntityManagerIsFilterEnabled(): void
$this->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): void
{
// Add filters to the configuration of the EM
Expand Down Expand Up @@ -276,7 +302,6 @@ public function testSQLFilterGetSetParameter(): void
$filter = new MyLocaleFilter($em);

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

$this->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 @@ -76,6 +76,17 @@ public function testGetFilter(): void

$this->assertInstanceOf(MyFilter::class, $filterCollection->getFilter('testFilter'));
}

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 895172f

Please sign in to comment.