这是我的控制器
public function index2Action($name)
{
$em = $this->getDoctrine()->getEntityManager();
$test = $em->getRepository('RestWebServiceBundle:Test')->findall();
return new Response(json_encode(array('locations' => $test)));
}
当我转到 URL 时,我得到:
{"locations":[{}]}
但是,当我使用:
public function index2Action($name)
{
$name ="Adam";
return new Response(json_encode(array('locations' => $name)));
}
我得到了 JSON。
我究竟做错了什么?我正在尝试在第一种情况下获取 JSON。
更新:我已经验证 $test 变量确实不为空,当我对其执行 print_r 时,它显示以下内容:
Array
(
[0] => Rest\WebServiceBundle\Entity\Test Object
(
[id:protected] => 1
[title:protected] => test title
[author:protected] => test author
[blog:protected] => this is the blog
[tags:protected] =>
[comments:protected] =>
[created:protected] => DateTime Object
(
[date] => 2012-05-13 00:00:00
[timezone_type] => 3
[timezone] => America/Chicago
)
[updated:protected] => DateTime Object
(
[date] => 2012-05-13 00:00:00
[timezone_type] => 3
[timezone] => America/Chicago
)
)
)
null