1
1
# Handling Relations to Collections
2
2
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 `
9
4
relation between them (through the ` authors ` property).
10
5
11
- This API can be created using the following PHP code:
6
+ This API using the following PHP code:
12
7
13
8
``` php
14
9
<?php
@@ -74,22 +69,29 @@ class Book
74
69
}
75
70
```
76
71
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).
78
80
79
81
``` javascript
80
82
import React , { Component } from ' react' ;
81
83
import { ReferenceArrayField , SingleFieldList , ChipField , ReferenceArrayInput , SelectArrayInput } from ' react-admin' ;
82
84
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' ;
84
86
85
87
const entrypoint = ' https://demo.api-platform.com' ;
86
88
87
89
export default class extends Component {
88
- state = {api: null , resources : null };
90
+ state = { api: null };
89
91
90
92
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 );
93
95
94
96
// Set the field in the list and the show views
95
97
books .readableFields .find (f => ' authors' === f .name ).fieldComponent =
@@ -107,20 +109,19 @@ export default class extends Component {
107
109
< / ReferenceArrayInput>
108
110
;
109
111
110
- this .setState ({api, resources });
112
+ this .setState ({ api });
111
113
}
112
114
)
113
115
}
114
116
115
117
render () {
116
118
if (null === this .state .api ) return < div> Loading... < / div> ;
117
119
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 ) }/ >
119
121
}
120
122
}
121
123
```
122
124
123
- The admin now properly handles this ` to-many ` relation!
124
125
125
126
## Using an Autocomplete Input for Relations
126
127
0 commit comments