@@ -89,6 +89,7 @@ protected function POST($action = '', $data = '') {
89
89
$ ents = [$ ents ];
90
90
}
91
91
$ created = [];
92
+ $ hadSuccess = false ;
92
93
$ invalidData = false ;
93
94
foreach ($ ents as $ entData ) {
94
95
if ((int ) $ entData ['guid ' ] > 0 ) {
@@ -103,14 +104,18 @@ protected function POST($action = '', $data = '') {
103
104
try {
104
105
if ($ entity ->save ()) {
105
106
$ created [] = $ entity ;
107
+ $ hadSuccess = true ;
108
+ } else {
109
+ $ created [] = false ;
106
110
}
107
111
} catch (Exceptions \EntityInvalidDataException $ e ) {
108
112
$ invalidData = true ;
113
+ $ created [] = null ;
109
114
} catch (\Exception $ e ) {
110
115
return $ this ->httpError (500 , 'Internal Server Error ' , $ e );
111
116
}
112
117
}
113
- if (empty ( $ created ) ) {
118
+ if (! $ hadSuccess ) {
114
119
if ($ invalidData ) {
115
120
return $ this ->httpError (400 , 'Bad Request ' );
116
121
} else {
@@ -168,7 +173,11 @@ protected function POST($action = '', $data = '') {
168
173
$ args ['params ' ]
169
174
);
170
175
header ('Content-Type: application/json ' );
171
- echo json_encode (['entity ' => $ entity , 'return ' => $ ret ]);
176
+ if ($ args ['stateless ' ]) {
177
+ echo json_encode (['return ' => $ ret ]);
178
+ } else {
179
+ echo json_encode (['entity ' => $ entity , 'return ' => $ ret ]);
180
+ }
172
181
} catch (\Exception $ e ) {
173
182
return $ this ->httpError (500 , 'Internal Server Error ' , $ e );
174
183
}
@@ -232,6 +241,7 @@ protected function doPutOrPatch($action, $data, $patch) {
232
241
$ ents = [$ ents ];
233
242
}
234
243
$ saved = [];
244
+ $ hadSuccess = false ;
235
245
$ invalidData = false ;
236
246
$ notfound = false ;
237
247
$ lastException = null ;
@@ -248,14 +258,18 @@ protected function doPutOrPatch($action, $data, $patch) {
248
258
try {
249
259
if ($ entity ->save ()) {
250
260
$ saved [] = $ entity ;
261
+ $ hadSuccess = true ;
262
+ } else {
263
+ $ saved [] = false ;
251
264
}
252
265
} catch (Exceptions \EntityInvalidDataException $ e ) {
253
266
$ invalidData = true ;
267
+ $ saved [] = null ;
254
268
} catch (\Exception $ e ) {
255
269
$ lastException = $ e ;
256
270
}
257
271
}
258
- if (empty ( $ saved ) ) {
272
+ if (! $ hadSuccess ) {
259
273
if ($ invalidData ) {
260
274
return $ this ->httpError (400 , 'Bad Request ' );
261
275
} elseif ($ notfound ) {
@@ -468,7 +482,7 @@ protected function loadEntity($entityData, $patch = false) {
468
482
* @return boolean Always returns false.
469
483
*/
470
484
protected function httpError ($ errorCode , $ message , $ exception = null ) {
471
- header ( " HTTP/1.1 $ errorCode $ message " , true , $ errorCode );
485
+ http_response_code ( $ errorCode );
472
486
if ($ exception ) {
473
487
echo json_encode (
474
488
[
0 commit comments