Api平台请求问题 [英] Api-platform request issue

查看:28
本文介绍了Api平台请求问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体作者和书

book |多对一 |作者身份证 ||idid作者 |

在作者控制器中,我有一个 Action 函数来获取作者的详细信息和他的所有书籍(使用 forward 函数获取书籍)

公共函数authorDetailsAction($id){$author= $this->getDoctrine()->getRepository(Author::class)->find($id);$books= $this->forward('AppBundle:Book:authorBooks',array('id' => $id));return array('author' => $author, 'books' => $books);}

<块引用>

但它只显示作者信息

或者当我只查看书籍 return $books 我收到这个错误

<块引用>

控制器必须返回一个响应 (Array(0 => Object(AppBundle\Entity\Book), 1 => Object(AppBundle\Entity\Book), 2 => Object(AppBundle\Entity\Book)) 给出).(500 内部服务器错误)

提前感谢您的指导

解决方案

就像您使用 API 平台一样,您可以在不创建自定义控制器的情况下实现您想要的:

  1. Author 资源添加过滤器,以便能够按姓名搜索作者:https://api-platform.com/docs/core/serialization-groups-and-relations#embedding-relations
  2. 直接在作者回复中嵌入与作者相关的书籍:https://api-platform.com/docs/core/filters#search-filter.

I have two entities author and book

book     | manytoOne |author
id       |           |id
idAuthor |

in the author controller i have an Action function that gets the author details and all his books (using the forward function to get the books)

public function authorDetailsAction($id){

$author= $this->getDoctrine()->getRepository(Author::class)->find($id);

$books= $this->forward('AppBundle:Book:authorBooks',array('id' => $id));

return array('author' => $author, 'books' => $books);

}

but it shows only author informations

or when im rutrning only the books return $books i got this error

The controller must return a response (Array(0 => Object(AppBundle\Entity\Book), 1 => Object(AppBundle\Entity\Book), 2 => Object(AppBundle\Entity\Book)) given). (500 Internal Server Error)

Thanks in advance for any guidance

解决方案

As it looks like you use API Platform, you can achieve what you want without creating a custom controller:

  1. Add a filter to the Author resource to be able to search an author by name: https://api-platform.com/docs/core/serialization-groups-and-relations#embedding-relations
  2. Embed books related to an author directly in the author response: https://api-platform.com/docs/core/filters#search-filter.

这篇关于Api平台请求问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆