Api平台请求问题 [英] Api-platform request issue
问题描述
我有两个实体作者和书
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 平台一样,您可以在不创建自定义控制器的情况下实现您想要的:
- 向
Author
资源添加过滤器,以便能够按姓名搜索作者:https://api-platform.com/docs/core/serialization-groups-and-relations#embedding-relations - 直接在作者回复中嵌入与作者相关的书籍: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:
- 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 - Embed books related to an author directly in the author response: https://api-platform.com/docs/core/filters#search-filter.
这篇关于Api平台请求问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!