Doctrine - 使用查询构建器时如何合并集合 [英] Doctrine - How to hydrate a collection when using query builder

查看:173
本文介绍了Doctrine - 使用查询构建器时如何合并集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上一个问题我问在使用Doctrine和查询构建器时对结果集进行了水化。我的问题是如何返回一个数组及其子集:

A previous question I asked was to do with hydrating a result set when using Doctrine and query builder. My issue was how to return an array and their sub-sets:

这是一个单一的结果集,答案很简单:

This was for a single result set and the answer was quite simple:

    $qb = $this->stoneRepository->createQueryBuilder('S');

        $query = $qb->addSelect('A','P','I','C')
            ->leftJoin('S.attribute', 'A')
            ->leftJoin('A.category', 'C')
            ->innerJoin('S.product' , 'P')
            ->innerJoin('S.image' , 'I')
            ->where('S.id = :sid')
            ->setParameter('sid', (int) $stone_id)
            ->getQuery();

        $resultArray = $query->getOneOrNullResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);

        return $resultArray;

我的下一个问题是如何为集合做相同的事情?这是我试过的:

My next question is how to do this exact same thing for a collection? This is what I have tried:

public function fetchAll()
    {
        $qb   = $this->stoneRepository->createQueryBuilder('S');

        $qb->addSelect('A','P','I','C')
            ->leftJoin('S.attribute', 'A')
            ->leftJoin('A.category', 'C')
            ->innerJoin('S.product' , 'P')
            ->innerJoin('S.image' , 'I')
            ->where('S.state=:state')
            ->setParameter('state' , 1 );

        $adapter    = new DoctrineAdapter( new ORMPaginator( $qb ) );
        $collection = new StoneCollection($adapter);

        return $collection;
    }

我使用此解决方案遇到的问题是连接表不在

The problem I am facing with this solution is that the join tables are not being populated and I am ending up with a collection of empty results.

StoneCollection类简单地扩展了paginator:

The StoneCollection class simply extends paginator:

<?php
namespace Api\V1\Rest\Stone;

use Zend\Paginator\Paginator;

class StoneCollection extends Paginator
{

}

我想,也许最好的方法是获取一个数组并打开数组?

I am thinking that perhaps the best mehod is to get an array and to page the array?

:

我有这个工作,虽然我不热衷于它,因为我打了DB两次。第一次构建数组(这是整个结果集,对于某些应用程序可能是非常大的),然后第二次页面结果,然后返回到Api中的HAL进行处理...

I have this working although I am not keen on it as I hit the DB twice. The first time to build the array (Which is the entire result set which could be very big for some applications) and then the second time to page the results which is then returned to HAL in ApiGility for processing...

理想情况下,这应该是一回事,但我不知道如何在一个单一的实例中将结果水平...

Ideally this should be done in one go however I am not sure how to hydrate the results in a single instance...

public function fetchAll( $page = 1 )
{
    $qb   = $this->stoneRepository->createQueryBuilder('S');

    $qb->addSelect('A','P','I','C')
        ->leftJoin('S.attribute', 'A')
        ->leftJoin('A.category', 'C')
        ->innerJoin('S.product' , 'P')
        ->innerJoin('S.image' , 'I')
        ->where('S.state=:state')
        ->setParameter('state' , 1 );

    $resultArray = $qb->getQuery()->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);

    $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($resultArray));
    $paginator->setCurrentPageNumber($page);

    return $paginator;
}


推荐答案

我有以上:

我有这个工作,虽然我不热衷于它,因为我打了DB两次。第一次构建数组(这是整个结果集,对于某些应用程序可能是非常大的),然后第二次页面结果,然后返回到Api中的HAL进行处理...

I have this working although I am not keen on it as I hit the DB twice. The first time to build the array (Which is the entire result set which could be very big for some applications) and then the second time to page the results which is then returned to HAL in ApiGility for processing...

理想情况下,这应该是一回事,但我不知道如何在一个单一的实例中将结果水平...

Ideally this should be done in one go however I am not sure how to hydrate the results in a single instance...

public function fetchAll( $page = 1 )
{
    $qb   = $this->stoneRepository->createQueryBuilder('S');

    $qb->addSelect('A','P','I','C')
        ->leftJoin('S.attribute', 'A')
        ->leftJoin('A.category', 'C')
        ->innerJoin('S.product' , 'P')
        ->innerJoin('S.image' , 'I')
        ->where('S.state=:state')
        ->setParameter('state' , 1 );

    $resultArray = $qb->getQuery()->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);

    $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($resultArray));
    $paginator->setCurrentPageNumber($page);

    return $paginator;
}

这篇关于Doctrine - 使用查询构建器时如何合并集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆