1

I have no idea why, but after a few (unrelated) changes all my returns are objects instead of arrays. For example:

ReceiptController.php

/**
 * Get all receipts for the team.
 *
 * @param String $id
 * @return array
 *
 * @SWG\Response(
 *     response=201,
 *     description="Get all receipt for the team.",
 *     @SWG\Schema(
 *         type="array"
 *     )
 * )
 */
public function getTeamReceiptsAction($id): array
{
    $parameterBag = [
        'teamId' => $id
    ];

    $items = $this->manager->getAll($parameterBag);

    return $items;
}

Receiptrepository

/**
 * @inheritdoc
 */
public function getFilterQuery($parameterBag): QueryBuilder
{
    $qb = $this
        ->createQueryBuilder('s')
    ;

    if (isset($parameterBag['id'])) {
        $qb->andWhere('s.id=(:id)')
            ->setParameter('id', $parameterBag['id']);
    }

    if (isset($parameterBag['teamId'])) {
        $qb->andWhere('s.team=(:team)')
            ->setParameter('team', $parameterBag['teamId']);
    }

    return $qb;
}


/**
 * @inheritdoc
 */
public function getAll($parameterBag): ?array
{
    $qb = $this->getFilterQuery($parameterBag);

    return $qb->getQuery()->getResult();
}

Returns: {"0":{"email":"[email protected]","first_name"... While i would expect [{"email":"[email protected]","first_name"... ?

So it returns an object with multiple objects in it instead of an array.

But that's weird, because all my functions are typed to return array's, and I don't get any errors?

2
  • Do a dump(get_class($items)) and see what kind of object it is. Could be an object that implements the various array interfaces which in turn will pass the array type tests. Commented Jun 22, 2017 at 12:06
  • If I do that, I get the following Internal Error: {"code":500,"message":"Warning: get_class() expects parameter 1 to be object, array given"}. Interesting. Commented Jun 22, 2017 at 12:20

1 Answer 1

2

try to change this:

return $qb->getQuery()->getResult();

to this:

return $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);

In this mode you can retrieve results in array

Or as Tomasz Madeysk wrote try this:

return $qb->getQuery()->getArrayResult()
Sign up to request clarification or add additional context in comments.

2 Comments

or another way to accomplish that: getQuery()->getArrayResult() (it's the same under the hood)
yes, I prefer Query::HYDRATE_ARRAY but is another good solution!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.