Important
You are browsing the documentation for version 4.1 of OroCommerce, OroCRM and OroPlatform, which is no longer maintained. Read version 5.1 (the latest LTS version) of the Oro documentation to get up-to-date information.
See our Release Process documentation for more information on the currently supported and upcoming releases.
Creating and Updating Related Resources with Primary API Resource¶
Sometimes, it is required to create or update the related resources while creating or updating the primary API resource, especially when creating a complex API resource object via a single API request. Unfortunately, the JSON:API specification does not describe how to do this. The solution proposed by OroPlatform is to use the included section of a JSON request body as in the example below:
Request
1POST /api/accounts HTTP/1.1
2Content-Type: application/vnd.api+json
3Accept: application/vnd.api+json
Request Body
1{"data": {
2 "type": "accounts",
3 "id": "1",
4 "attributes": {
5 "name": "Cloth World"
6 },
7 "relationships": {
8 "organization": {
9 "data": {"type": "organizations", "id": "1"}
10 },
11 "owner": {
12 "data": {"type": "users", "id": "1"}
13 },
14 "contacts": {
15 "data": [
16 {"type": "contacts", "id": "8da4d8e7-6b25-4c5c-8075-b510f7bbb84f"},
17 {"type": "contacts", "id": "707dda0d-35f5-47b9-b2ce-a3e92b9fdee7"}
18 ]
19 },
20 "defaultContact": {
21 "data": {"type": "contacts", "id": "707dda0d-35f5-47b9-b2ce-a3e92b9fdee7"}
22 }
23 }
24 },
25 "included": [
26 {
27 "type": "contacts",
28 "id": "8da4d8e7-6b25-4c5c-8075-b510f7bbb84f",
29 "attributes": {
30 "firstName": "John",
31 "lastName": "Doe"
32 },
33 "relationships": {
34 "organization": {
35 "data": {"type": "organizations", "id": "1"}
36 },
37 "owner": {
38 "data": {"type": "users", "id": "1"}
39 },
40 "source": {
41 "data": {"type": "contactsources", "id": "tv"}
42 }
43 }
44 },
45 {
46 "type": "contacts",
47 "id": "707dda0d-35f5-47b9-b2ce-a3e92b9fdee7",
48 "attributes": {
49 "firstName": "Nancy",
50 "lastName": "Jones"
51 },
52 "relationships": {
53 "organization": {
54 "data": {"type": "organizations", "id": "1"}
55 },
56 "owner": {
57 "data": {"type": "users", "id": "1"}
58 }
59 }
60 }
61 ]
62}
Response
1{"data": {
2 "type": "accounts",
3 "id": "52",
4 "attributes": {
5 "name": "Cloth World"
6 },
7 "relationships": {
8 "organization": {
9 "data": {"type": "organizations", "id": "1"}
10 },
11 "owner": {
12 "data": {"type": "users", "id": "1"}
13 },
14 "contacts": {
15 "data": [
16 {"type": "contacts", "id": "79"},
17 {"type": "contacts", "id": "80"}
18 ]
19 },
20 "defaultContact": {
21 "data": {"type": "contacts", "id": "80"}
22 }
23 }
24 },
25 "included": [
26 {
27 "type": "contacts",
28 "id": "79",
29 "meta": {
30 "includeId": "8da4d8e7-6b25-4c5c-8075-b510f7bbb84f"
31 },
32 "attributes": {
33 "firstName": "John",
34 "lastName": "Doe"
35 },
36 "relationships": {
37 "organization": {
38 "data": {"type": "organizations", "id": "1"}
39 },
40 "owner": {
41 "data": {"type": "users", "id": "1"}
42 },
43 "source": {
44 "data": {"type": "contactsources", "id": "tv"}
45 },
46 "accounts": {
47 "data": [
48 {"type": "accounts", "id": "52"}
49 ]
50 }
51 }
52 },
53 {
54 "type": "contacts",
55 "id": "80",
56 "meta": {
57 "includeId": "707dda0d-35f5-47b9-b2ce-a3e92b9fdee7"
58 },
59 "attributes": {
60 "firstName": "Nancy",
61 "lastName": "Jones"
62 },
63 "relationships": {
64 "organization": {
65 "data": {"type": "organizations", "id": "1"}
66 },
67 "owner": {
68 "data": {"type": "users", "id": "1"}
69 },
70 "accounts": {
71 "data": [
72 {"type": "accounts", "id": "52"}
73 ]
74 }
75 }
76 }
77 ]
78}
This request does the following:
Creates the account ‘Cloth World’.
Creates two contacts, ‘John Doe’ and ‘Nancy Jones’.
Assigns these contacts to the account ‘Cloth World’.
Makes ‘Nancy Jones’ the default contact for ‘Cloth World’.
Please pay attention to the identifiers of the contacts. For ‘John Doe’ it is ‘8da4d8e7-6b25-4c5c-8075-b510f7bbb84f’. For ‘Nancy Jones’ it is ‘707dda0d-35f5-47b9-b2ce-a3e92b9fdee7’. These identifiers are used to specify relations between resources in scope of the request. In this example, GUIDs are used, but it is possible to use any algorithm to generate such identifiers. The only requirement is that they must be unique in scope of the request. For example, the following identifiers are valid as well: ‘john_doe’ and ‘nancy_jones’.
Also, it is possible to update several related resources via a single API request. The related resources to be updated should be marked with update meta property. For instance, take a look at the following request:
Request
1PATCH /api/accounts/52 HTTP/1.1
2Content-Type: application/vnd.api+json
3Accept: application/vnd.api+json
Request Body
1{"data": {
2 "type": "accounts",
3 "id": "52",
4 "attributes": {
5 "name": "Cloth World Market"
6 },
7 "relationships": {
8 "defaultContact": {
9 "data": {"type": "contacts", "id": "79"}
10 }
11 }
12 },
13 "included": [
14 {
15 "meta": {
16 "update": true
17 },
18 "type": "contacts",
19 "id": "79",
20 "attributes": {
21 "primaryEmail": "john_doe@example.com"
22 }
23 }
24 ]
25}
Response
1{"data": {
2 "type": "accounts",
3 "id": "52",
4 "attributes": {
5 "name": "Cloth World Market"
6 },
7 "relationships": {
8 "defaultContact": {
9 "data": {"type": "contacts", "id": "79"}
10 }
11 }
12 },
13 "included": [
14 {
15 "type": "contacts",
16 "id": "79",
17 "meta": {
18 "includeId": "79"
19 },
20 "attributes": {
21 "primaryEmail": "john_doe@example.com"
22 }
23 }
24 ]
25}
This request does the following:
Changes the account name to ‘Cloth World Market’.
Sets the primary email for the contact with identifier ‘79’ and makes it the default contact.
All included resources in а request must be connected to the primary resource and this must be explicitly specified in the request. You can specify the connection in both ways, from the primary resource to the included resource, and vise versa. The following examples are equivalent:
1{"data": {
2 "type": "accounts",
3 "id": "52",
4 "relationships": {
5 "contact": {
6 "data": {"type": "contacts", "id": "79"}
7 }
8 }
9 },
10 "included": [
11 {
12 "meta": {
13 "update": true
14 },
15 "type": "contacts",
16 "id": "79",
17 "attributes": {
18 "primaryEmail": "john_doe@example.com"
19 }
20 }
21 ]
22}
1{"data": {
2 "type": "accounts",
3 "id": "52"
4 },
5 "included": [
6 {
7 "meta": {
8 "update": true
9 },
10 "type": "contacts",
11 "id": "79",
12 "attributes": {
13 "primaryEmail": "john_doe@example.com"
14 },
15 "relationships": {
16 "account": {
17 "data": {"type": "accounts", "id": "52"}
18 }
19 }
20 }
21 ]
22}