REST API - batch create possible with one server call?

Hello,

I am building a REST API with Yii2. I’ve followed along with the guide and created a working controller which works great when I submit one json object. The attributes are successfully applied to the modelClass defined in the controller and saved. My validation rules are followed and it returns the error messages if the validation fails.

However, I was hoping if I passed an array of json objects (each of which I want to be individually applied to my modelClass) to be saved as sort of a the validation fails of course as the run function in the action doesn’t know how to deal with an array. Is there a way to do a batch insert when passing a json object?

My first attempt at this was to write my own action which was just a version of \yii\rest\CreateAction where I overwrote the create action to call my new action. Then in the run function I implemented the logic of iterating through the array and setting each item to the associated model to validate and save, but the problem with that being is I want this all in a transaction since if any of the input fails validation I don’t want to save any of it. And in that case I wasn’t sure how I should handle the return error messages.

Does this functionality already exist and I’m just missing something? Or does anyone have any insight/suggestions how they would handle a similar situation?