我正试图在字段"action"中从我的DB中提取一个值,它是一个JSON字符串,但我现在将其存储为单个值,这就是:
'command'=>'get','target'=>'location'
然而,当我从数据库中提取它时,它包含了我不想要的字段名,请参见以下内容:
[{"action":"'command'=>'get','target'=>'location'"}]
我的代码在这里:
$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));
所以我只需要知道如何获取不包括字段名称的字段值?
尝试此方法getSingleScalarResult()
但请记住,如果它找不到任何东西,就会抛出异常
http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html#single-标量水合
您希望使用getSingleResult()
而不是getResult()
来获取字段的值。如果没有找到结果或有多个结果,它将抛出异常(setMaxResults(1)
将修复此部分)。
https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/AbstractQuery.php#L802