Repository navigation
Add subresource docs - #199
Conversation
| } | ||
| ``` | ||
|
|
||
| If the relation you put the subresource on is a `ToMany`, you will retrieve a collection. |
There was a problem hiding this comment.
If you put the subresource on a relation that is toMany, you will retrieve...
adb9fb2 to
2930b49
Compare
dunglas
left a comment
There was a problem hiding this comment.
I would also write item and resource (without uppercased first letter) for consistency.
| /** | ||
| * Answer. | ||
| * | ||
| * @ORM\Table(name="answer") |
There was a problem hiding this comment.
This annotation is useless and should be removed.
| class Answer | ||
| { | ||
| /** | ||
| * @ORM\Column(name="id", type="integer") |
There was a problem hiding this comment.
The name attribute is useless and should be removed.
| private $id; | ||
|
|
||
| /** | ||
| * @ORM\Column(name="content", type="string", nullable=false) |
There was a problem hiding this comment.
All attributes are useless and should be removed.
| use Doctrine\ORM\Mapping as ORM; | ||
|
|
||
| /** | ||
| * @ORM\Table(name="question") |
There was a problem hiding this comment.
The @Table annotation is useless and should be removed.
| class Question | ||
| { | ||
| /** | ||
| * @ORM\Column(name="id", type="integer") |
There was a problem hiding this comment.
The name attribute is useless and should be removed.
|
|
||
| /** | ||
| * @ORM\OneToOne(targetEntity="Answer", inversedBy="question") | ||
| * @ORM\JoinColumn(name="answer_id", referencedColumnName="id", unique=true) |
There was a problem hiding this comment.
The name attribute is useless and should be removed.
| /** | ||
| * @ORM\OneToOne(targetEntity="Answer", inversedBy="question") | ||
| * @ORM\JoinColumn(name="answer_id", referencedColumnName="id", unique=true) | ||
| * @ApiProperty(subresource=true) |
There was a problem hiding this comment.
I would put this annotation first to increase its visibility.
| /** | ||
| * @ORM\OneToOne(targetEntity="Question", mappedBy="answer") | ||
| */ | ||
| private $question; |
There was a problem hiding this comment.
Missing getters/setters (or the property must be public). Same for Question.
| } | ||
| ``` | ||
|
|
||
| Note that all we had to do is to set up `@ApiProperty(subresource=true)` on the `Question::answer` relation. Because the `answer` is a `OneToOne` relation, we know that this subresource is an Item. Therefore the response will look like this: |
There was a problem hiding this comment.
we have to do?
To set up the @ApiProperty(subresource=true) annotation?
Question::$answer property.
|
|
||
| You may want custom groups on subresources. Because a subresource is nothing more than a collection operation, you can set `normalization_context` or `denormalization_context` on that operation. To do so, you need to override `collectionOperations`. Base on the above operation, because we retrieve an answer, we need to alter it's configuration: | ||
|
|
||
| <configurations> |
There was a problem hiding this comment.
I don't think that this that this tag is supported.
There was a problem hiding this comment.
we' re using it above though.
|
Thanks for the review @dunglas, still we may need to rethink how subresources are declared (see our recent discussion on the subject with @teohhanhui).
|
|
this PR needs updating for api-platform/core#1206 |
|
Bumped with @ApiSubresource and comments fixing. |
| } | ||
| ``` | ||
|
|
||
| If you put the subresource on a relation that is `ToMany`, you will retrieve a collection. |
| } | ||
| ``` | ||
|
|
||
| Note that all we have to do is to set up `@ApiSubresource` annotation on the `Question::answer` property. Because the `answer` is a `OneToOne` relation, we know that this subresource is an Item. Therefore the response will look like this: |
|
|
||
| If you put the subresource on a relation that is `ToMany`, you will retrieve a collection. | ||
|
|
||
| Last but not least, subresources can be nested so that `/foos/1/bars/1/foobars` will get `foobars` belonging to `bar=1` itself belonging to `foo=1`. To do this, just add as many subresources annotations as you need. |
There was a problem hiding this comment.
Subresources can be nested, such that /questions/42/answer/comments will get the collection of comments for the answer to question 42.
|
|
||
| Since ApiPlatform 2.1, you can declare subresources. A "subresource" is a resource exposed under a path with parameters dependent on another resource. It can result in a collection or an item. | ||
|
|
||
| For example, let's create two entities (Question, Answer) and set up a subresource so that `/question/42/answer` gives us the answer to the question `42`: |
There was a problem hiding this comment.
questions/42/answer
the question `42` -> question 42
| ```json | ||
| { | ||
| "@context": "/contexts/Answer", | ||
| "@id": "/answers/1", |
| "@id": "/answers/1", | ||
| "@type": "Answer", | ||
| "id": 42, | ||
| "content": "42", |
There was a problem hiding this comment.
Life, the Universe, and Everything 😛
a6c027f to
faca475
Compare
|
Bump |
|
|
||
| ## Subresources | ||
|
|
||
| Since ApiPlatform 2.1, you can declare subresources. A subresource is a collection or an item that belongs to another resource. The starting point of a resource must be a relation on an existing resource. |
There was a problem hiding this comment.
The starting point of a subresource ... ?
faca475 to
3389a6c
Compare
6e06d5a to
9f5edf7
Compare
9f5edf7 to
c9e6c5e
Compare
No description provided.