2

Doctrine 2 具有 Doctrine\ORM\Tools\Pagination\Paginator 类,可用于对普通 DQL 查询进行分页。

但是,如果我将本机查询传递给它,则会收到此错误:

Catchable fatal error: Argument 1 passed to Doctrine\ORM\Tools\Pagination\Paginator::cloneQuery() must be an instance of Doctrine\ORM\Query, instance of Doctrine\ORM\NativeQuery given

我尝试在 cloneQuery 方法中从分页器类中删除类型提示,但这只会产生更多错误,因为分页器类的其他位期望在 Query 中找到的方法不在 NativeQuery 中。

是否有任何简单的方法可以对本机查询进行分页,而无需构建新的分页器类或将数据库中的每一行都提取到数组中?

4

3 回答 3

8

我制作了自己的与 Zend_Paginator 兼容的分页器适配器类。

可能不会是最灵活的,因为它依赖于在查询开始附近有一个“FROM”(参见 count() 方法),但它是一个相对快速和简单的修复。

/**
 * Paginate native doctrine 2 queries 
 */
class NativePaginator implements Zend_Paginator_Adapter_Interface
{
    /**
     * @var Doctrine\ORM\NativeQuery
     */
    protected $query;
    protected $count;

    /**
     * @param Doctrine\ORM\NativeQuery $query 
     */
    public function __construct($query)
    {
        $this->query = $query;
    }

    /**
     * Returns the total number of rows in the result set.
     *
     * @return integer
     */
    public function count()
    {
        if(!$this->count)
        {
            //change to a count query by changing the bit before the FROM
            $sql = explode(' FROM ', $this->query->getSql());
            $sql[0] = 'SELECT COUNT(*)';
            $sql = implode(' FROM ', $sql);

            $db = $this->query->getEntityManager()->getConnection();
            $this->count = (int) $db->fetchColumn($sql, $this->query->getParameters());
        }

        return $this->count;
    }

    /**
     * Returns an collection of items for a page.
     *
     * @param  integer $offset Page offset
     * @param  integer $itemCountPerPage Number of items per page
     * @return array
     */
    public function getItems($offset, $itemCountPerPage)
    {
        $cloneQuery = clone $this->query;
        $cloneQuery->setParameters($this->query->getParameters(), $this->query->getParameterTypes());

        foreach($this->query->getHints() as $name => $value)
        {
            $cloneQuery->setHint($name, $value);
        }

        //add on limit and offset
        $sql = $cloneQuery->getSQL();
        $sql .= " LIMIT $itemCountPerPage OFFSET $offset";
        $cloneQuery->setSQL($sql);

        return $cloneQuery->getResult();
    }
}
于 2013-01-22T17:10:27.697 回答
0

如果你有一个dbal query builder你已经构建的

$yourDbalQueryBuilder = $connection->createQueryBuilder();

那么你可以使用:

$yourDbalQueryBuilder->setFirstResult(0)
                     ->setMaxResults(100000000)
                     ->execute()
                     ->rowCount();
于 2016-09-14T17:35:34.377 回答
0
public function countTotalRecords($query, string $primaryKey = '*'): int
    {
        if ($query instanceof QueryBuilder) {
            $paginator = new Paginator($query->getQuery());
            return count($paginator);
        } else if ($query instanceof NativeQuery) {
            $rsm = new ResultSetMappingBuilder($query->getEntityManager());
            $rsm->addScalarResult('count', 'count');

            $sqlCount = "select count(".$primaryKey.") as count from (" . $query->getSQL() . ") as item";

            $count = $query->getEntityManager()->createNativeQuery($sqlCount, $rsm);

            if ($query->getParameter('limit')) {
                $query->setParameter('limit', null);
            }

            $count->setParameters($query->getParameters());

            return (int)$count->getSingleScalarResult();
        }
        return 0;
    }
于 2021-03-09T22:23:04.203 回答