Skip to content

Commit 3b3ffbf

Browse files
authored
add maxDepth on subresources doc
1 parent f8b0a1a commit 3b3ffbf

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

core/operations.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,49 @@ Note that the operation name, here `api_questions_answer_get_subresource`, is th
344344
It'll be automatically set to `$resources_$subresource(s)_get_subresource`. To find the correct operation name you
345345
may use `bin/console debug:router`.
346346

347+
Add to that, you can control depth of subresources with the parameter `maxDepth`. For example, if `Answer` entity also have subresource such as `comments`, you would like to not generate the route `api/questions/{id}/answers/{id}/comments`. You can do this by adding the parameter maxDepth in ApiSubresource annotation or yml/xml file configuration.
348+
349+
```php
350+
<?php
351+
// src/AppBundle/Entity/Question.php
352+
353+
use ApiPlatform\Core\Annotation\ApiProperty;
354+
use ApiPlatform\Core\Annotation\ApiResource;
355+
use ApiPlatform\Core\Annotation\ApiSubresource;
356+
use Doctrine\ORM\Mapping as ORM;
357+
358+
/**
359+
* @ORM\Entity
360+
* @ApiResource
361+
*/
362+
class Question
363+
{
364+
/**
365+
* @ORM\Column(type="integer")
366+
* @ORM\Id
367+
* @ORM\GeneratedValue(strategy="AUTO")
368+
*/
369+
private $id;
370+
371+
/**
372+
* @ORM\Column
373+
*/
374+
public $content;
375+
376+
/**
377+
* @ORM\OneToOne(targetEntity="Answer", inversedBy="question")
378+
* @ORM\JoinColumn(referencedColumnName="id", unique=true)
379+
* @ApiSubresource(maxDepth=1)
380+
*/
381+
public $answer;
382+
383+
public function getId()
384+
{
385+
return $this->id;
386+
}
387+
}
388+
```
389+
347390
## Creating Custom Operations and Controllers
348391

349392
API Platform can leverage the Symfony routing system to register custom operation related to custom controllers. Such custom

0 commit comments

Comments
 (0)