2016-05-02 14 views
0

APIエンドポイントの一部として写真のリストを返す必要があります。SymfonyのAPI呼び出しの特定のフィールドのみを返します

しかし、Symfonyのクエリビルダによって返されたオブジェクトのリストを返そうとすると、各写真オブジェクトも同様にすべてのCHILDデータが返されます。写真に添付されたユーザーに関するデータ。これは返されたデータをひどく膨らませます。

APIエンドポイントで特定のフィールドのみが返されるように、オブジェクトのリストを選択またはフィルタリングするにはどうすればよいですか?あなたはこのようなものを使用することができます

public function getManyAction(Request $request, $card_id) { 
    $photos = $this->getDoctrine()->getRepository('AppBundle:Photo')->findByCard($card_id); 
    if($photos) { 
     $response = $this->serializeResponseToJson(true, null, $photos); 
    } 
    else{ 
     $response = $this->serializeResponseToJson(false, "No photos were found for this card"); 
    } 

    return new JsonResponse($response); 

} 

答えて

1

... $em=$this->getDoctrine()->getManager(); $query=$em->createQuery('SELECT p.field1,p.field2,p.field3 FROM AppBundle:Photo p WHERE p.card=:card')->setParameter('card',$card_id); $photos=$query->getResult(); ... return new JsonResponse($response);

+0

は素晴らしい作品を、ありがとう – emkay

関連する問題