Skip to content

Commit d703c93

Browse files
committed
Update handling-relations-to-collections
1 parent af19930 commit d703c93

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

admin/handling-relations-to-collections.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
# Handling Relations to Collections
22

3-
Currently, API Platform Admin doesn't handle `to-many` relations. The core library [is being patched](https://github.com/api-platform/core/pull/1189)
4-
to document relations to collections through OWL.
5-
6-
Meanwhile, it is possible to manually configure API Platform to handle relations to collections.
7-
8-
We will create the admin for an API exposing `Person` and `Book` resources linked with a `many-to-many`
3+
Considering an API exposing `Person` and `Book` resources linked with a `many-to-many`
94
relation between them (through the `authors` property).
105

11-
This API can be created using the following PHP code:
6+
This API using the following PHP code:
127

138
```php
149
<?php
@@ -74,22 +69,29 @@ class Book
7469
}
7570
```
7671

77-
Let's customize the components used for the `authors` property:
72+
The admin handles this `to-many` relation automatically!
73+
74+
But we can go further:
75+
76+
77+
## Customizing a property
78+
79+
Let's customize the components used for the `authors` property, to display them by their 'name' instead 'id' (the default behavior).
7880

7981
```javascript
8082
import React, { Component } from 'react';
8183
import { ReferenceArrayField, SingleFieldList, ChipField, ReferenceArrayInput, SelectArrayInput } from 'react-admin';
8284
import { AdminBuilder, hydraClient } from '@api-platform/admin';
83-
import parseHydraDocumentation from 'api-doc-parser/lib/hydra/parseHydraDocumentation';
85+
import parseHydraDocumentation from '@api-platform/api-doc-parser/lib/hydra/parseHydraDocumentation';
8486

8587
const entrypoint = 'https://demo.api-platform.com';
8688

8789
export default class extends Component {
88-
state = {api: null, resources: null};
90+
state = { api: null };
8991

9092
componentDidMount() {
91-
parseHydraDocumentation(entrypoint).then({api, resources} => {
92-
const books = r.find(r => 'books' === r.name);
93+
parseHydraDocumentation(entrypoint).then( ({api}) => {
94+
const books = api.resources.find(r => 'books' === r.name);
9395

9496
// Set the field in the list and the show views
9597
books.readableFields.find(f => 'authors' === f.name).fieldComponent =
@@ -107,20 +109,19 @@ export default class extends Component {
107109
</ReferenceArrayInput>
108110
;
109111

110-
this.setState({api, resources});
112+
this.setState({ api });
111113
}
112114
)
113115
}
114116

115117
render() {
116118
if (null === this.state.api) return <div>Loading...</div>;
117119

118-
return <AdminBuilder api={this.state.api} dataProvider={hydraClient({entrypoint: entrypoint, resources: this.state.resources})}/>
120+
return <AdminBuilder api={ this.state.api } dataProvider={ hydraClient(this.state.api) }/>
119121
}
120122
}
121123
```
122124

123-
The admin now properly handles this `to-many` relation!
124125

125126
## Using an Autocomplete Input for Relations
126127

index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@
143143
3. [Using a Custom Validation Function or Inject Custom Props](admin/getting-started.md#using-a-custom-validation-function-or-inject-custom-props)
144144
3. [Authentication Support](admin/authentication-support.md)
145145
4. [Handling Relations to Collections](admin/handling-relations-to-collections.md)
146-
1. [Using an Autocomplete Input for Relations](admin/handling-relations-to-collections.md#using-an-autocomplete-input-for-relations)
146+
1. [Customizing a property](admin/handling-relations-to-collections.md#customizing-a-property)
147+
2. [Using an Autocomplete Input for Relations](admin/handling-relations-to-collections.md#using-an-autocomplete-input-for-relations)
147148

148149
## The Client Generator Component: Scaffold Progressive Web Apps and Native Apps
149150

0 commit comments

Comments
 (0)