1
0
Fork 0
mirror of synced 2025-04-01 12:26:11 +03:00

Merge branch 'feature/default-query-hints' of https://github.com/Strate/doctrine2 into Strate-default-query-hints

This commit is contained in:
Guilherme Blanco 2014-05-15 01:15:34 +00:00
commit 4874070b3f
4 changed files with 77 additions and 2 deletions

View file

@ -167,6 +167,7 @@ abstract class AbstractQuery
{
$this->_em = $em;
$this->parameters = new ArrayCollection();
$this->_hints = $em->getConfiguration()->getDefaultQueryHints();
$this->hasCache = $this->_em->getConfiguration()->isSecondLevelCacheEnabled();
if ($this->hasCache) {
@ -299,7 +300,7 @@ abstract class AbstractQuery
{
$this->parameters = new ArrayCollection();
$this->_hints = array();
$this->_hints = $this->_em->getConfiguration()->getDefaultQueryHints();
}
/**
@ -1089,6 +1090,7 @@ abstract class AbstractQuery
$this->parameters = new ArrayCollection();
$this->_hints = array();
$this->_hints = $this->_em->getConfiguration()->getDefaultQueryHints();
}
/**

View file

@ -858,4 +858,57 @@ class Configuration extends \Doctrine\DBAL\Configuration
? $this->_attributes['secondLevelCacheConfiguration']
: null;
}
/**
* Returns query hints, which will be applied to every query in application
*
* @since 2.5
*
* @return array
*/
public function getDefaultQueryHints()
{
return isset($this->_attributes['defaultQueryHints']) ? $this->_attributes['defaultQueryHints'] : array();
}
/**
* Sets array of query hints, which will be applied to every query in application
*
* @since 2.5
*
* @param array $defaultQueryHints
*/
public function setDefaultQueryHints(array $defaultQueryHints)
{
$this->_attributes['defaultQueryHints'] = $defaultQueryHints;
}
/**
* Gets the value of a default query hint. If the hint name is not recognized, FALSE is returned.
*
* @since 2.5
*
* @param string $name The name of the hint.
*
* @return mixed The value of the hint or FALSE, if the hint name is not recognized.
*/
public function getDefaultQueryHint($name)
{
return isset($this->_attributes['defaultQueryHints'][$name])
? $this->_attributes['defaultQueryHints'][$name]
: false;
}
/**
* Sets a default query hint. If the hint name is not recognized, it is silently ignored.
*
* @since 2.5
*
* @param string $name The name of the hint.
* @param mixed $value The value of the hint.
*/
public function setDefaultQueryHint($name, $value)
{
$this->_attributes['defaultQueryHints'][$name] = $value;
}
}

View file

@ -683,7 +683,7 @@ final class Query extends AbstractQuery
ksort($this->_hints);
return md5(
$this->getDql() . var_export($this->_hints, true) .
$this->getDql() . serialize($this->_hints) .
($this->_em->hasFilters() ? $this->_em->getFilters()->getHash() : '') .
'&firstResult=' . $this->_firstResult . '&maxResult=' . $this->_maxResults .
'&hydrationMode='.$this->_hydrationMode.'DOCTRINE_QUERY_CACHE_SALT'

View file

@ -5,10 +5,12 @@ namespace Doctrine\Tests\ORM\Query;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query\Parameter;
class QueryTest extends \Doctrine\Tests\OrmTestCase
{
/** @var EntityManager */
protected $_em = null;
protected function setUp()
@ -177,4 +179,22 @@ class QueryTest extends \Doctrine\Tests\OrmTestCase
$query->processParameterValue($this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'))
);
}
public function testDefaultQueryHints()
{
$config = $this->_em->getConfiguration();
$defaultHints = array(
'hint_name_1' => 'hint_value_1',
'hint_name_2' => 'hint_value_2',
'hint_name_3' => 'hint_value_3',
);
$config->setDefaultQueryHints($defaultHints);
$query = $this->_em->createQuery();
$this->assertSame($config->getDefaultQueryHints(), $query->getHints());
$this->_em->getConfiguration()->setDefaultQueryHint('hint_name_1', 'hint_another_value_1');
$this->assertNotSame($config->getDefaultQueryHints(), $query->getHints());
$q2 = clone $query;
$this->assertSame($config->getDefaultQueryHints(), $q2->getHints());
}
}