1
0
mirror of synced 2025-01-19 15:01:40 +03:00

Merge pull request #967 from FabioBatSilva/slc-query-builder

[SLC] Add query builder options
This commit is contained in:
Guilherme Blanco 2014-03-03 21:10:48 -05:00
commit 15432fc55f
2 changed files with 176 additions and 8 deletions

View File

@ -118,6 +118,32 @@ class QueryBuilder
*/
private $joinRootAliases = array();
/**
* Whether to use second level cache, if available.
*
* @var boolean
*/
protected $cacheable = false;
/**
* Second level cache region name.
*
* @var string|null
*/
protected $cacheRegion;
/**
* Second level query cache mode.
*
* @var integer|null
*/
protected $cacheMode;
/**
* @var integer
*/
protected $lifetime = 0;
/**
* Initializes a new <tt>QueryBuilder</tt> that uses the given <tt>EntityManager</tt>.
*
@ -151,6 +177,91 @@ class QueryBuilder
return $this->_em->getExpressionBuilder();
}
/**
*
* Enable/disable second level query (result) caching for this query.
*
* @param boolean $cacheable
*
* @return \Doctrine\ORM\AbstractQuery This query instance.
*/
public function setCacheable($cacheable)
{
$this->cacheable = (boolean) $cacheable;
return $this;
}
/**
* @return boolean TRUE if the query results are enable for second level cache, FALSE otherwise.
*/
public function isCacheable()
{
return $this->cacheable;
}
/**
* @param string $cacheRegion
*
* @return \Doctrine\ORM\AbstractQuery This query instance.
*/
public function setCacheRegion($cacheRegion)
{
$this->cacheRegion = (string) $cacheRegion;
return $this;
}
/**
* Obtain the name of the second level query cache region in which query results will be stored
*
* @return The cache region name; NULL indicates the default region.
*/
public function getCacheRegion()
{
return $this->cacheRegion;
}
/**
* @return integer
*/
public function getLifetime()
{
return $this->lifetime;
}
/**
* Sets the life-time for this query into second level cache.
*
* @param integer $lifetime
* @return \Doctrine\ORM\AbstractQuery This query instance.
*/
public function setLifetime($lifetime)
{
$this->lifetime = (integer) $lifetime;
return $this;
}
/**
* @return integer
*/
public function getCacheMode()
{
return $this->cacheMode;
}
/**
* @param integer $cacheMode
* @return \Doctrine\ORM\AbstractQuery This query instance.
*/
public function setCacheMode($cacheMode)
{
$this->cacheMode = (integer) $cacheMode;
return $this;
}
/**
* Gets the type of the currently built query.
*
@ -236,11 +347,28 @@ class QueryBuilder
public function getQuery()
{
$parameters = clone $this->parameters;
return $this->_em->createQuery($this->getDQL())
$query = $this->_em->createQuery($this->getDQL())
->setParameters($parameters)
->setFirstResult($this->_firstResult)
->setMaxResults($this->_maxResults);
if ($this->lifetime) {
$query->setLifetime($this->lifetime);
}
if ($this->cacheMode) {
$query->setCacheMode($this->cacheMode);
}
if ($this->cacheable) {
$query->setCacheable($this->cacheable);
}
if ($this->cacheRegion) {
$query->setCacheRegion($this->cacheRegion);
}
return $query;
}
/**

View File

@ -19,13 +19,14 @@
namespace Doctrine\Tests\ORM;
use Doctrine\Common\Collections\ArrayCollection,
Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Cache;
use Doctrine\ORM\QueryBuilder,
Doctrine\ORM\Query\Expr,
Doctrine\ORM\Query\Parameter,
Doctrine\ORM\Query\ParameterTypeInferer;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\Query\Parameter;
use Doctrine\ORM\Query\ParameterTypeInferer;
require_once __DIR__ . '/../TestInit.php';
@ -978,4 +979,43 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase
->add('where', 'u.bar = ?2', true)
;
}
public function testSecondLevelCacheQueryBuilderOptions()
{
$defaultQueryBuilder = $this->_em->createQueryBuilder()
->select('s')
->from('Doctrine\Tests\Models\Cache\State', 's');
$this->assertFalse($defaultQueryBuilder->isCacheable());
$this->assertEquals(0, $defaultQueryBuilder->getLifetime());
$this->assertNull($defaultQueryBuilder->getCacheRegion());
$this->assertNull($defaultQueryBuilder->getCacheMode());
$defaultQuery = $defaultQueryBuilder->getQuery();
$this->assertFalse($defaultQuery->isCacheable());
$this->assertEquals(0, $defaultQuery->getLifetime());
$this->assertNull($defaultQuery->getCacheRegion());
$this->assertNull($defaultQuery->getCacheMode());
$builder = $this->_em->createQueryBuilder()
->select('s')
->setLifetime(123)
->setCacheable(true)
->setCacheRegion('foo_reg')
->setCacheMode(Cache::MODE_REFRESH)
->from('Doctrine\Tests\Models\Cache\State', 's');
$this->assertTrue($builder->isCacheable());
$this->assertEquals(123, $builder->getLifetime());
$this->assertEquals('foo_reg', $builder->getCacheRegion());
$this->assertEquals(Cache::MODE_REFRESH, $builder->getCacheMode());
$query = $builder->getQuery();
$this->assertTrue($query->isCacheable());
$this->assertEquals(123, $query->getLifetime());
$this->assertEquals('foo_reg', $query->getCacheRegion());
$this->assertEquals(Cache::MODE_REFRESH, $query->getCacheMode());
}
}