これは私のコントローラーです
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