假设我有两个实体:Book和Author,它们是这样设置的:
Author.php
/**
* @ApiResource(attributes={
* "normalization_context"={"groups"={"author"}, "enable_max_depth"=true},
* "denormalization_context"={"groups"={"author"}, "enable_max_depth"=true}
* })
* @ORM\Entity(repositoryClass="App\Repository\AuthorRepository")
*/
class Author
{
...
/**
* @ORM\OneToMany(targetEntity="App\Entity\Book", mappedBy="author", orphanRemoval=true)
* @Groups({"author", "book"})
* @MaxDepth(3)
*/
private $books;
Book.php
/**
* @ApiResource(attributes={
* "normalization_context"={"groups"={"book"}, "enable_max_depth"=true},
* "denormalization_context"={"groups"={"book"}, "enable_max_depth"=true}
* })
* @ORM\Entity(repositoryClass="App\Repository\BookRepository")
*/
class Book
{
...
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Author", inversedBy="books")
* @ORM\JoinColumn(nullable=false)
* @Groups({"book", "author"})
* @MaxDepth(3)
*/
private $author;
我想和他的作者一起拿一本书,以及他写的所有书。这样一来,我以后就可以遍历这本书。
问题是当我请求url时:
GET localhost:8001/api/books/1
响应如下:
{
"@context": "/api/contexts/Book",
"@id": "/api/books/1",
"@type": "Book",
"author": {
"@id": "/api/authors/1",
"@type": "Author",
"books": [
"/api/books/1",
{
"@id": "/api/books/2",
"@type": "Book",
"author": "/api/authors/1"
}
]
}
}
我想把这本书1作为对象而不是IRI,我搞砸了什么?
edit:澄清一下,我期望的答复是:
{
"@context": "/api/contexts/Book",
"@id": "/api/books/1",
"@type": "Book",
"author": {
"@id": "/api/authors/1",
"@type": "Author",
"books": [
{
"@id": "/api/books/1",
"@type": "Book",
"author": "/api/authors/1"
},
{
"@id": "/api/books/2",
"@type": "Book",
"author": "/api/authors/1"
}
]
}
}