Created
August 23, 2011 08:29
-
-
Save damienalexandre/1164649 to your computer and use it in GitHub Desktop.
This file is not part of the Pagerfanta package. We hack the Pagerfanta to allow Doctrine nativeQuery pagination.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This file is not part of the Pagerfanta package. | |
* We hack the Pagerfanta to allow Doctrine nativeQuery pagination. | |
*/ | |
namespace Dalexandre\MyBundle\Pagerfanta\Adapter; | |
use Doctrine\ORM\QueryBuilder; | |
use Doctrine\ORM\Query; | |
use Doctrine\ORM\NoResultException; | |
use \Doctrine\ORM\NativeQuery; | |
/** | |
* DoctrineORMNativeQueryAdapter. | |
* | |
* Ok so this class is a real tweak, here is some advice: | |
* - don't use it with INNER JOIN query | |
* - don't use it with query already LIMITED | |
* - don't use it. Haha. | |
* | |
* @experimental | |
* @todo : Deal with a fetchJoinCollection. | |
*/ | |
class DoctrineORMNativeQueryAdapter implements \Pagerfanta\Adapter\AdapterInterface | |
{ | |
/** | |
* @var Query | |
*/ | |
private $query; | |
private $fetchJoinCollection; | |
/** | |
* @param \Doctrine\ORM\NativeQuery $query | |
* @param bool $fetchJoinCollection | |
*/ | |
public function __construct(NativeQuery $query, $fetchJoinCollection = false) | |
{ | |
$this->query = $query; | |
$this->fetchJoin = (Boolean) $fetchJoinCollection; | |
} | |
/** | |
* Returns the query | |
* | |
* @return NativeQuery | |
* | |
* @api | |
*/ | |
public function getQuery() | |
{ | |
return $this->query; | |
} | |
/** | |
* Returns whether the query joins a collection. | |
* | |
* @return Boolean Whether the query joins a collection. | |
*/ | |
public function getFetchJoinCollection() | |
{ | |
$this->fetchJoinCollection; | |
} | |
public function getNbResults() | |
{ | |
$sql = $this->getQuery()->getSQL(); | |
$sql = preg_replace('@^SELECT .+ FROM@i', 'SELECT COUNT(*) as total FROM', $sql); | |
$count = $this->getQuery()->getEntityManager()->getConnection()->fetchColumn($sql, $this->getQuery()->getParameters()); | |
return $count; | |
} | |
public function getSlice($offset, $length) | |
{ | |
if ($this->fetchJoinCollection) { | |
// @todo | |
} | |
$query = clone $this->getQuery(); | |
$query->setParameters( $this->getQuery()->getParameters() ); | |
$sql = $query->getSql(); | |
$sql .= sprintf(' LIMIT %d, %d', $offset, $length); | |
$query->setSql($sql); | |
return $query->getResult(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The best line is the 21 ahahah