|
| 1 | +# Handling Relations to Collections |
| 2 | + |
| 3 | +API Platform Admin handles `to-one` and `to-many` relations automatically. |
| 4 | +Thanks to [the Schema.org support](schema.org.md), it's also easy to display the name of a related objects instead of its IRI. |
| 5 | + |
| 6 | +Let's go one step further thanks to the [customization capabilities](customizing.md) of API Platform Admin by adding autocompletion support to form inputs. |
| 7 | + |
| 8 | +## Using an Autocomplete Input for Relations |
| 9 | + |
| 10 | +Let's consider an API exposing `Person` and `Book` resources linked by a `many-to-many` |
| 11 | +relation (through the `authors` property). |
| 12 | + |
| 13 | +This API uses the following PHP code: |
| 14 | + |
| 15 | +```php |
| 16 | +<?php |
| 17 | +// api/src/Entity/Person.php |
| 18 | + |
| 19 | +namespace App\Entity; |
| 20 | + |
| 21 | +use ApiPlatform\Core\Annotation\ApiFilter; |
| 22 | +use ApiPlatform\Core\Annotation\ApiResource; |
| 23 | +use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter; |
| 24 | +use Doctrine\ORM\Mapping as ORM; |
| 25 | + |
| 26 | +/** |
| 27 | + * @ApiResource |
| 28 | + * @ORM\Entity |
| 29 | + */ |
| 30 | +class Person |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @ORM\Column(type="integer") |
| 34 | + * @ORM\GeneratedValue |
| 35 | + * @ORM\Id |
| 36 | + */ |
| 37 | + public $id; |
| 38 | + |
| 39 | + /** |
| 40 | + * @ORM\Column |
| 41 | + * @ApiFilter(SearchFilter::class, strategy="ipartial") |
| 42 | + */ |
| 43 | + public $name; |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +```php |
| 48 | +<?php |
| 49 | +// api/src/Entity/Book.php |
| 50 | + |
| 51 | +namespace App\Entity; |
| 52 | + |
| 53 | +use ApiPlatform\Core\Annotation\ApiResource; |
| 54 | +use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter; |
| 55 | +use Doctrine\Common\Collections\ArrayCollection; |
| 56 | +use Doctrine\ORM\Mapping as ORM; |
| 57 | + |
| 58 | +/** |
| 59 | + * @ApiResource |
| 60 | + * @ORM\Entity |
| 61 | + */ |
| 62 | +class Book |
| 63 | +{ |
| 64 | + /** |
| 65 | + * @ORM\Column(type="integer") |
| 66 | + * @ORM\GeneratedValue |
| 67 | + * @ORM\Id |
| 68 | + */ |
| 69 | + public $id; |
| 70 | + |
| 71 | + /** |
| 72 | + * @ORM\ManyToMany(targetEntity="Person") |
| 73 | + */ |
| 74 | + public $authors; |
| 75 | + |
| 76 | + public function __construct() |
| 77 | + { |
| 78 | + $this->authors = new ArrayCollection(); |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +Notice the "partial search" [filter](../core/filters.md) on the `name` property of the `Book` resource class. |
| 84 | + |
| 85 | +Now, let's configure API Platform Admin to enable autocompletion for the relation selector: |
| 86 | + |
| 87 | +```javascript |
| 88 | +import React from "react"; |
| 89 | +import { |
| 90 | + HydraAdmin, |
| 91 | + ResourceGuesser, |
| 92 | + CreateGuesser, |
| 93 | + EditGuesser, |
| 94 | + InputGuesser |
| 95 | +} from "@api-platform/admin"; |
| 96 | +import { ReferenceInput, AutocompleteInput } from "react-admin"; |
| 97 | + |
| 98 | +const ReviewsCreate = props => ( |
| 99 | + <CreateGuesser {...props}> |
| 100 | + <InputGuesser source="author" /> |
| 101 | + <ReferenceInput |
| 102 | + source="book" |
| 103 | + reference="books" |
| 104 | + label="Books" |
| 105 | + filterToQuery={searchText => ({ title: searchText })} |
| 106 | + > |
| 107 | + <AutocompleteInput optionText="title" /> |
| 108 | + </ReferenceInput> |
| 109 | + |
| 110 | + <InputGuesser source="rating" /> |
| 111 | + <InputGuesser source="body" /> |
| 112 | + <InputGuesser source="publicationDate" /> |
| 113 | + </CreateGuesser> |
| 114 | +); |
| 115 | + |
| 116 | +const ReviewsEdit = props => ( |
| 117 | + <EditGuesser {...props}> |
| 118 | + <InputGuesser source="author" /> |
| 119 | + |
| 120 | + <ReferenceInput |
| 121 | + source="book" |
| 122 | + reference="books" |
| 123 | + label="Books" |
| 124 | + filterToQuery={searchText => ({ title: searchText })} |
| 125 | + > |
| 126 | + <AutocompleteInput optionText="title" /> |
| 127 | + </ReferenceInput> |
| 128 | + |
| 129 | + <InputGuesser source="rating" /> |
| 130 | + <InputGuesser source="body" /> |
| 131 | + <InputGuesser source="publicationDate" /> |
| 132 | + </EditGuesser> |
| 133 | +); |
| 134 | + |
| 135 | +export default () => ( |
| 136 | + <HydraAdmin entrypoint={process.env.REACT_APP_API_ENTRYPOINT}> |
| 137 | + <ResourceGuesser |
| 138 | + name="reviews" |
| 139 | + create={ReviewsCreate} |
| 140 | + edit={ReviewsEdit} |
| 141 | + /> |
| 142 | + </HydraAdmin> |
| 143 | +); |
| 144 | +``` |
| 145 | + |
| 146 | +The autocomplete field should now work properly! |
0 commit comments