Clément Andraud
Clément Andraud

Reputation: 9269

Symfony2 Doctrine, remove useless array in query

I have a query in a repository like :

        $qb->select('f.state as link')
            ->from('MyProjectBundle:Friend', 'f')
            ->where('CONDITIONS');
        return $qb->getQuery()->getResult();

So, this query return the state with you and a friend. In my controller i return :

return array('link' => $state);

$state is the result of my query. I can only have one result with this query, it's just the state with ONE other user. So, when i check the response, i have (in json) :

    {
    "link": [
        {
            "link": 1
        }
    ]    
   }

How can i remove the array [] in this json ? It's useless here.. Thanks !

Upvotes: 0

Views: 99

Answers (2)

krichprollsch
krichprollsch

Reputation: 391

You can use getSingleScalarResult() method as explained here :

http://docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html#query-result-formats

$qb->select('f.state as link')
    ->from('MyProjectBundle:Friend', 'f')
    ->where('CONDITIONS');

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

Upvotes: 1

Fabien Papet
Fabien Papet

Reputation: 2319

As expected ;)
You can use return array(current($state));

Upvotes: 1

Related Questions