Hi,
I have an issue where I need to POST multiple values, but in the response, the HAL collection name has defaulted to “items”. The GET works correctly and gives the correct response.
How do I get the POST to provide the correct collection name?
Very simple example to hopefully demostrate my issue.
In the resource i have the typical fetchAll and create functions, for demo purposes I have hardcoded the values.
The fetchAll function:
public function fetchAll($params = [])
{
$result = array();
$result[0]->id= 1;
$result[0]->name= 'name1';
$result[1]->id= 2;
$result[1]->name= 'name2';
foreach($result as $row) {
$entities[]= new WidgetsEntity($row);
}
$adapter = new ArrayAdapter($entities);
$collection = new WidgetsCollection($adapter);
return $collection;
}
Returns:
{
“_links”: {
“self”: {
“href”: “/demo-services/widgets?page=1”
},
“first”: {
“href”: “/demo-services/widgets”
},
“last”: {
“href”: “/demo-services/widgets?page=1”
}
},
“_embedded”: {
“widgets”: [
{
“id”: 1,
“name”: “name1”,
“_links”: {
“self”: {
“href”: “/demo-services/widgets/1”
}
}
},
{
“id”: 2,
“name”: “name2”,
“_links”: {
“self”: {
“href”: “/demo-services/widgets/2”
}
}
}
]
},
“page_count”: 1,
“page_size”: 25,
“total_items”: 2,
“page”: 1
}
The create function, using the exact same code as the fetchAll gives the following with the “items” instead of “widgets”
public function create($data)
{
$result = array();
$result[0]->id= 1;
$result[0]->name= 'name1';
$result[1]->id= 2;
$result[1]->name= 'name2';
foreach($result as $row) {
$entities[]= new WidgetsEntity($row);
}
$adapter = new ArrayAdapter($entities);
$collection = new WidgetsCollection($adapter);
return $collection;
}
Returns:
{
“_links”: {
“self”: {
“href”: “/demo-services/widgets?page=1”
},
“first”: {
“href”: “/demo-services/widgets”
},
“last”: {
“href”: “/demo-services/widgets?page=1”
}
},
“_embedded”: {
“items”: [
{
“id”: 1,
“name”: “name1”,
“_links”: {
“self”: {
“href”: “/demo-services/widgets/1”
}
}
},
{
“id”: 2,
“name”: “name2”,
“_links”: {
“self”: {
“href”: “/demo-services/widgets/2”
}
}
}
]
},
“page_count”: 1,
“page_size”: 30,
“total_items”: 2,
“page”: 1
}
Why is it doing this and how do I replace “items” with “widgets”?
Any help would be appreciated here.