1

I'm trying to pull a value from my DB in the field 'action', it is a JSON string however I'm storing it as a single value for now, this is it:

'command'=>'get','target'=>'location'

However when I pull it from the DB it includes the field name, which I don't want, see below:

[{"action":"'command'=>'get','target'=>'location'"}]

My code is here:

$em = $this->getDoctrine()->getManager();   
    $query = $em->createQueryBuilder();
    $q = $query->select('z.action')
    ->from('AppBundle:ZeusUsers', 'z')
    ->where('z.id = ?1')
    ->setParameter(1, $id)
    ->getQuery();


    $action = $q->getResult();
    return new Response(json_encode($action));

So I just need to know how to grab the field value not including the field name?

2 Answers 2

1

try this method getSingleScalarResult()

but remember that if it wouldn't find anything it will throw exception

http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html#single-scalar-hydration

Sign up to request clarification or add additional context in comments.

Comments

0

You want to use getSingleResult() instead of getResult() to get value of your field. It will throw exception if there are no results found or there are more than one result (setMaxResults(1) will remedy this part) though.

https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/AbstractQuery.php#L802

1 Comment

Doesn't seem to make a difference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.