Empty collection arrays from doctrine within apigility fetchAll($params) resource method

Inside my class ItemResource I have method which fetch rows from db with doctrine:

public function fetchAll($params)
{
    $query = $this->entityManager->getRepository(UserItem::class)->getItems();
    
    $adapter = new DoctrineAdapter(new ORMPaginator($query, false));
    $paginator = new Paginator($adapter);
   
    $items = $paginator;

    return $items;
}

When I open url /api/rest/user/item inside browser I se such results:

{
    "_links":{"self":{"href":"\/api\/rest\/v1\/user\/item?page=1"},
    "first":{"href":"\/api\/rest\/v1\/user\/item"},
    "last":{"href":"\/api\/rest\/v1\/user\/item?page=1"}},
    "_embedded":{
        "item":[
            [],
            [],
            []
        ]
    },
    "page_count":1,
    "page_size":25,
    "total_items":3,
    "page":1
}

Why my _embedded.item contains empty arrays?

When I try to print fields value with this code:

foreach($items as $item){
    echo get_class($item) . '<br>';
    
    echo $item->getId() . '<br>';
    echo $item->getUsername() . '<br>';
}

it returns right values as I expected.

What happens when you try to json_encode one of your entities? Is it an empty array?

That might work for you

echo json_encode($item)
and
echo json_encode($items)
returns empty braces:
{}

This code returns the same empty result as my previous code:

return new ItemCollection($paginator->getAdapter());

Let’s try a slightly different approach

public function fetchAll($params) {
    $queryBuilder = $this->entityManager->createQueryBuilder();
    $queryBuilder->select('item')->from(UserItem::class, 'item');

    $query = $queryBuilder->createQuery();
    $query->setDQL($queryBuilder->getDQL())->setHydrationMode(Query::HYDRATE_ARRAY);

    $ormPaginator = new ORMPaginator($query);
    $ormPaginator->setUseOutputWalkers(false);

    $adapter = new Paginator(new DoctrinePaginator($ormPaginator));

    return new Collection(new ItemCollection($adapter->getAdapter()));
}
2 Likes

I am getting this error:

"Call to undefined method Doctrine\\ORM\\QueryBuilder::createQuery()"

I have slightly modified your example and I have got what I expected. This is my version of code:

public function fetchAll($params) {
    $queryBuilder = $this->entityManager->createQueryBuilder();
    $queryBuilder->select('item')->from(UserItem::class, 'item');

    $query = $queryBuilder->getQuery();
    $query->setDQL($queryBuilder->getDQL())->setHydrationMode(Query::HYDRATE_ARRAY);

    $ormPaginator = new ORMPaginator($query);
    $ormPaginator->setUseOutputWalkers(false);

    $adapter = new Paginator(new DoctrineOrmAdapter($ormPaginator));

    return new ItemCollection($adapter->getAdapter());
}

Without this line:

 $query->setDQL($queryBuilder->getDQL())->setHydrationMode(Query::HYDRATE_ARRAY);

I get empty arrays. It looks like, collection don’t want to work properly without hydration mode.

Guido, thank you!

I typed all of the above without looking at actual code.
So, yes, there is a chance I messed up somewhere :smile:

You’re welcome!