【发布时间】:2016-05-02 19:39:01
【问题描述】:
我需要返回一个照片列表作为 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);
}
【问题讨论】: