Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added getHash method in SQLFilter to avoid caching the filtered queries #8393

Open
wants to merge 1 commit into
base: old-prototype-3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spl_object_hash is not reliable within and across requests. You need to serialize the parameter. the API of setParameter only allows stirngs anyways, it is not enforced though. But that should allow us to do this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about changing this method to

final public function getHash()
{
        return self::class.$this;
}

I don't think the serialize() will have a big performance impact here.

} 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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add an object based parameter and see serialize.

{
$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