0

Given a json string:

POST localhost:8080/person  
{
   f_name : 'Juan',  
   l_name : 'Dela Cruz'  
}

we would usually return a boolean true value upon success so that apigility would return/render something like this:

200 Ok  
{  
    f_name : 'Juan',  
    l_name : 'Dela Cruz'  
}

How can I also include the id of the newly created entity in the resulting response? It may look something like this:

200 Ok  
{  
    id : 1,
    f_name : 'Juan',  
    l_name : 'Dela Cruz'  
}

Any help/lead is highly appreciated.

leonard.javiniar
  • 539
  • 8
  • 25

2 Answers2

0

I am including my own answer here, but I think this is not the best possible way.

I have discovered that you can also return an array from the create (POST) method of the resource with the 'id' field taken from the lastInsertValue property of an db adapter object and added into the array.

$insert['f_name'] = 'Juan';  
$insert['l_name'] = 'Dela Cruz';

$table = new TableGateway('person', $adapter);
$table->insert($insert);

$insert['id'] = $table->lastInsertValue;

return $insert;

I hope there are still other ways to do this.

leonard.javiniar
  • 539
  • 8
  • 25
0

Apigility does indeed return the object you send. If you want to get a different result you can construct it yourself and return it as javiniar.leonard also pointed out.

Important is that you do not try to json_encode it but let Apigility handle that.

I use the API to send items created in an app with a local id, and return the local id and web_id to allow for synchronization on multiple devices.

Tom Groentjes
  • 1,053
  • 8
  • 7