これは私のコントローラはsymfonyでJSONを返すことができない2
public function index2Action($name)
{
$em = $this->getDoctrine()->getEntityManager();
$test = $em->getRepository('RestWebServiceBundle:Test')->findall();
return new Response(json_encode(array('locations' => $test)));
}
ですJSONを入手してください。
私は間違っていますか?私は最初のシナリオでJSONを取得しようとしています。
UPDATE:私は$テスト変数が実際に空でないことを確認したと、私はそれにします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
チェックを動作します。最初の例では、あなたはjsonを取り戻しています。 PHPはあなたが言うことだけを出力することができ、おそらく$ testは空のオブジェクトとして戻ってきます。だから...理由を見つけ出す。 –
テストで有効なレコードセットを取得していることを確認しました。 json_encodeとの関係は、findallの結果セットが好きではないような感じです。 – jini
json-encodeはオブジェクトを変換しますが、そのオブジェクトが "stringify"メソッドを提供する場合に限ります。 –