2011-05-09 18:48:25 +02:00
|
|
|
<?php
|
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
namespace FOS\ElasticaBundle\Doctrine\ORM;
|
2011-05-09 18:48:25 +02:00
|
|
|
|
2012-03-10 00:51:08 +01:00
|
|
|
use Doctrine\ORM\QueryBuilder;
|
2013-03-27 18:44:06 +01:00
|
|
|
use FOS\ElasticaBundle\Doctrine\AbstractProvider;
|
|
|
|
use FOS\ElasticaBundle\Exception\InvalidArgumentTypeException;
|
2011-05-10 00:31:20 +02:00
|
|
|
|
|
|
|
class Provider extends AbstractProvider
|
2011-05-09 18:48:25 +02:00
|
|
|
{
|
2013-06-19 12:36:59 +02:00
|
|
|
const ENTITY_ALIAS = 'a';
|
2014-03-19 13:40:52 +01:00
|
|
|
|
2014-03-31 07:45:31 +02:00
|
|
|
/**
|
|
|
|
* Disables logging and returns the logger that was previously set.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function disableLogging()
|
|
|
|
{
|
|
|
|
$configuration = $this->managerRegistry
|
|
|
|
->getManagerForClass($this->objectClass)
|
|
|
|
->getConnection()
|
|
|
|
->getConfiguration();
|
|
|
|
|
|
|
|
$logger = $configuration->getSQLLogger();
|
|
|
|
$configuration->setSQLLogger(null);
|
|
|
|
|
|
|
|
return $logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reenables the logger with the previously returned logger from disableLogging();
|
|
|
|
*
|
|
|
|
* @param mixed $logger
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function enableLogging($logger)
|
|
|
|
{
|
|
|
|
$configuration = $this->managerRegistry
|
|
|
|
->getManagerForClass($this->objectClass)
|
|
|
|
->getConnection()
|
|
|
|
->getConfiguration();
|
|
|
|
|
|
|
|
$configuration->setSQLLogger($logger);
|
|
|
|
}
|
|
|
|
|
2011-05-09 18:48:25 +02:00
|
|
|
/**
|
2013-03-27 18:44:06 +01:00
|
|
|
* @see FOS\ElasticaBundle\Doctrine\AbstractProvider::countObjects()
|
2012-03-10 00:51:08 +01:00
|
|
|
*/
|
2011-05-09 18:48:25 +02:00
|
|
|
protected function countObjects($queryBuilder)
|
|
|
|
{
|
2012-03-10 00:51:08 +01:00
|
|
|
if (!$queryBuilder instanceof QueryBuilder) {
|
|
|
|
throw new InvalidArgumentTypeException($queryBuilder, 'Doctrine\ORM\QueryBuilder');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clone the query builder before altering its field selection and DQL,
|
|
|
|
* lest we leave the query builder in a bad state for fetchSlice().
|
|
|
|
*/
|
2011-05-09 23:17:38 +02:00
|
|
|
$qb = clone $queryBuilder;
|
2013-03-28 12:15:02 +01:00
|
|
|
$rootAliases = $queryBuilder->getRootAliases();
|
2011-05-09 23:17:38 +02:00
|
|
|
|
2012-03-10 00:51:08 +01:00
|
|
|
return $qb
|
2013-03-28 12:15:02 +01:00
|
|
|
->select($qb->expr()->count($rootAliases[0]))
|
2012-03-10 00:51:08 +01:00
|
|
|
// Remove ordering for efficiency; it doesn't affect the count
|
|
|
|
->resetDQLPart('orderBy')
|
|
|
|
->getQuery()
|
|
|
|
->getSingleScalarResult();
|
2011-05-09 18:48:25 +02:00
|
|
|
}
|
2011-05-09 19:56:27 +02:00
|
|
|
|
|
|
|
/**
|
2013-03-27 18:44:06 +01:00
|
|
|
* @see FOS\ElasticaBundle\Doctrine\AbstractProvider::fetchSlice()
|
2012-03-10 00:51:08 +01:00
|
|
|
*/
|
2011-05-09 19:56:27 +02:00
|
|
|
protected function fetchSlice($queryBuilder, $limit, $offset)
|
|
|
|
{
|
2012-03-10 00:51:08 +01:00
|
|
|
if (!$queryBuilder instanceof QueryBuilder) {
|
|
|
|
throw new InvalidArgumentTypeException($queryBuilder, 'Doctrine\ORM\QueryBuilder');
|
|
|
|
}
|
2011-05-09 19:56:27 +02:00
|
|
|
|
2013-06-19 12:36:59 +02:00
|
|
|
/**
|
|
|
|
* An orderBy DQL part is required to avoid feching the same row twice.
|
|
|
|
* @see http://stackoverflow.com/questions/6314879/does-limit-offset-length-require-order-by-for-pagination
|
|
|
|
* @see http://www.postgresql.org/docs/current/static/queries-limit.html
|
|
|
|
* @see http://www.sqlite.org/lang_select.html#orderby
|
|
|
|
*/
|
|
|
|
$orderBy = $queryBuilder->getDQLPart('orderBy');
|
|
|
|
if (empty($orderBy)) {
|
2014-03-19 13:40:52 +01:00
|
|
|
$rootAliases = $queryBuilder->getRootAliases();
|
2013-06-19 12:36:59 +02:00
|
|
|
$identifierFieldNames = $this->managerRegistry
|
|
|
|
->getManagerForClass($this->objectClass)
|
|
|
|
->getClassMetadata($this->objectClass)
|
|
|
|
->getIdentifierFieldNames();
|
|
|
|
foreach ($identifierFieldNames as $fieldName) {
|
2014-03-19 13:40:52 +01:00
|
|
|
$queryBuilder->addOrderBy($rootAliases[0].'.'.$fieldName);
|
2013-06-19 12:36:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-10 00:51:08 +01:00
|
|
|
return $queryBuilder
|
|
|
|
->setFirstResult($offset)
|
|
|
|
->setMaxResults($limit)
|
|
|
|
->getQuery()
|
|
|
|
->getResult();
|
2011-05-09 23:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-27 18:44:06 +01:00
|
|
|
* @see FOS\ElasticaBundle\Doctrine\AbstractProvider::createQueryBuilder()
|
2012-03-10 00:51:08 +01:00
|
|
|
*/
|
2011-05-09 23:17:38 +02:00
|
|
|
protected function createQueryBuilder()
|
|
|
|
{
|
2012-03-10 00:51:08 +01:00
|
|
|
return $this->managerRegistry
|
2011-12-13 11:22:24 +01:00
|
|
|
->getManagerForClass($this->objectClass)
|
|
|
|
->getRepository($this->objectClass)
|
2012-03-10 00:51:08 +01:00
|
|
|
// ORM query builders require an alias argument
|
2013-06-19 12:36:59 +02:00
|
|
|
->{$this->options['query_builder_method']}(static::ENTITY_ALIAS);
|
2011-05-09 19:56:27 +02:00
|
|
|
}
|
2011-05-09 18:48:25 +02:00
|
|
|
}
|