0

これは私のコントローラーです

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
4

3 に答える 3

2

戻りエンティティにはシリアライザーを使用することを強くお勧めします。serializer コンポーネントまたは jmsserializerbundle を見てください。

于 2012-05-14T07:48:37.110 に答える
0

リポジトリクラスで getResult() の代わりに getArrayResult() を使用してこれを試しましたが、動作します

于 2014-03-26T08:56:55.830 に答える