Doctrine - How to Humidify a Collection Using the Query Builder

A previous question I asked was to inherit a result set when using Doctrine and the query builder. My problem was how to return the array and their subsets:

This was for one set of results, and the answer was pretty 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 exactly the same for the collection? This is what I 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 that I am encountering with this solution is that the connection tables are not populated, and I get a set of empty results.

The StoneCollection class simply extends the paginator:

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

use Zend\Paginator\Paginator;

class StoneCollection extends Paginator
{

}

I think that perhaps the best way is to get an array and enumerate the array?

EDIT ::

, , . ( , ), , HAL ApiGility ...

, , ...

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;
}
+2
1

, :

, , . ( , ), , HAL ApiGility ...

, , ...

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;
}
+1

Source: https://habr.com/ru/post/1568378/


All Articles