Doctrine Paginator Iteration是一个包含1个对象的数组

时间:2013-02-27 20:24:11

标签: symfony doctrine-orm doctrine pagination dql

我正在尝试使用Paginate类的学说来获取一些任务。

$qb = $this->getEntityManager()
            ->createQueryBuilder()
            ->select('DISTINCT task, priority, company, invoice')
            ->addSelect('priority.id as priority_id')
            ->from('TekniqSD4Bundle:task', 'task')
            ->leftJoin('task.slips', 'slips')
            ->leftJoin('task.comments', 'comments')
            ->leftJoin('task.files', 'files')
            ->leftJoin('task.steps', 'steps')
            ->leftJoin('task.status', 'status')
            ->join('task.invoice', 'invoice')
            ->join('task.priority', 'priority')
            ->join('invoice.company', 'company');

$query = $qb->getQuery()
            ->setMaxResults(2)  
            ->setFirstResult($offset);
$paginator = new Paginator($query, true);
foreach($paginator as $task){
    var_dump($task); //this spits out an array containing 1 task
}

我的问题是,为什么$ task是一个数组?

1 个答案:

答案 0 :(得分:3)

DQL中选定的priority.id导致此问题,因为ORM必须将其检索为标量结果。访问$task[0]应该会为您提供fetch-joined task个对象,而$task['priority_id']会为您提供代表priority.id的标量。