Skip to content

Commit b7f81fa

Browse files
committed
Bug fix
On save functions the current model is updated instead of being created fresh, so its able to be used after save. i.e. the passed by referenced model is returned instead of a new model.
1 parent 03d1260 commit b7f81fa

File tree

3 files changed

+217
-73
lines changed

3 files changed

+217
-73
lines changed

src/Support/Builder.php

Lines changed: 142 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ protected function getApiDataField()
169169
return $this->resource->getApiDataField();
170170
}
171171

172-
public function find($id, $column="")
172+
public function find($id, $column="")
173173
{
174174
if(is_array($id)){
175175
return $this->whereIn($id, $column)->get(null, $raw);
@@ -180,27 +180,27 @@ public function find($id, $column="")
180180
]), "individual", "allow");
181181
}
182182

183-
public function findMany(array $id, $column="")
183+
public function findMany(array $id, $column="")
184184
{
185185
return $this->whereIn($id, $column)->get();
186186
}
187187

188-
public function findOrFail($id)
188+
public function findOrFail($id)
189189
{
190190
return $this->handleResponse($this->sendRequest('get', [
191191
$this->retreiveEndPoint('get').'/'.$id
192192
]), "individual", "error");
193193
}
194194

195-
public function firstOrFail()
195+
public function firstOrFail()
196196
{
197197
if(!is_null($model = $this->first())){
198198
return $model;
199199
}
200200
throw (new ModelNotFoundException)->setModel(get_class($this->resource));
201201
}
202202

203-
protected function processHeaders()
203+
protected function processHeaders()
204204
{
205205
if($this->asForm){
206206
$this->request->asForm();
@@ -210,7 +210,7 @@ protected function processHeaders()
210210
}
211211
}
212212

213-
protected function processFiles()
213+
protected function processFiles()
214214
{
215215
if($this->files != []){
216216
foreach($this->files as $key => $file){
@@ -238,7 +238,7 @@ public function handleResponse($response, $type="individual", $ifEmpty="default"
238238
}
239239
}
240240

241-
public function handleRaw($response)
241+
public function handleRaw($response)
242242
{
243243
if(!$this->throwExceptionsIfRaw){
244244
return $response;
@@ -249,7 +249,7 @@ public function handleRaw($response)
249249
}
250250
}
251251

252-
public function handle404($response, $ifEmpty)
252+
public function handle404($response, $ifEmpty)
253253
{
254254
if($ifEmpty == 'allow'){
255255
return null;
@@ -260,7 +260,7 @@ public function handle404($response, $ifEmpty)
260260
}
261261
}
262262

263-
public function all()
263+
public function all()
264264
{
265265
$this->setPerPageToMax();
266266
if($this->resource->beforeQuery($this) === false){
@@ -270,7 +270,7 @@ public function all()
270270
$this->retreiveEndPoint('get'),
271271
$this->addPagination($this->combineQueries()),
272272
]), 'all');
273-
273+
274274
}
275275

276276
public function get()
@@ -304,7 +304,7 @@ public function post($attributes, $type="individual")
304304
$this->retreiveEndPoint('post'),
305305
$attributes,
306306
$this->combineQueries()
307-
]), $type);
307+
]), $type.'Post');
308308
}
309309

310310
public function patch($attributes, $type="individual")
@@ -316,7 +316,7 @@ public function patch($attributes, $type="individual")
316316
$this->retreiveEndPoint('patch'),
317317
$attributes,
318318
$this->combineQueries()
319-
]), $type);
319+
]), $type.'Patch');
320320
}
321321

322322
public function put($attributes, $type="individual")
@@ -328,18 +328,18 @@ public function put($attributes, $type="individual")
328328
$this->retreiveEndPoint('put'),
329329
$attributes,
330330
$this->combineQueries()
331-
]), $type);
331+
]), $type.'Put');
332332
}
333333

334-
public function delete($type="individual")
334+
public function delete($type="individual")
335335
{
336336
if($this->resource->beforeDeleteQuery($this) === false){
337337
return;
338338
}
339339
return $this->handleResponse($this->sendRequest('delete', [
340340
$this->retreiveEndPoint('delete'),
341341
$this->combineQueries()
342-
]), $type);
342+
]), $type.'Delete');
343343
}
344344

345345
public function first()
@@ -364,12 +364,12 @@ public function addQuery($key, $value)
364364
return $this;
365365
}
366366

367-
public function whereRaw($column, $value)
367+
public function whereRaw($column, $value)
368368
{
369369
$this->addQuery($column, $value);
370370
return $this;
371371
}
372-
372+
373373
public function where($column, $operand = null, $value = null)
374374
{
375375
if(is_array($column)){
@@ -433,7 +433,7 @@ protected function addWhereContains($column, $operand, $value)
433433
$this->wheres[] = ['column' => $column, 'operand' => $operand, 'value' => $value];
434434
}
435435

436-
public function whereIn(array $values, $column="")
436+
public function whereIn(array $values, $column="")
437437
{
438438
$string = implode(',', $values);
439439
if($column == ''){
@@ -443,24 +443,24 @@ public function whereIn(array $values, $column="")
443443
return $this;
444444
}
445445

446-
public function orderBy($value, $column='order')
446+
public function orderBy($value, $column='order')
447447
{
448448
$this->orders[$column] = $value;
449449
return $this;
450450
}
451451

452-
public function count()
452+
public function count()
453453
{
454454
return $this->get()->count();
455455
}
456456

457-
public function setPaginate($status=true)
457+
public function setPaginate($status=true)
458458
{
459459
$this->paginate = $status;
460460
return $this;
461461
}
462462

463-
public function shouldPaginate()
463+
public function shouldPaginate()
464464
{
465465
return $this->paginate;
466466
}
@@ -519,13 +519,13 @@ public function setPage($page)
519519
return $this;
520520
}
521521

522-
public function raw($status=true)
522+
public function raw($status=true)
523523
{
524524
$this->raw = $status;
525525
return $this;
526526
}
527527

528-
public function withExceptions($status=true)
528+
public function withExceptions($status=true)
529529
{
530530
$this->throwExceptionsIfRaw = $status;
531531
return $this;
@@ -536,7 +536,7 @@ public function isRaw()
536536
return $this->raw;
537537
}
538538

539-
protected function combineQueries()
539+
protected function combineQueries()
540540
{
541541
return array_merge($this->processQueries(), $this->processOrders());
542542
}
@@ -556,12 +556,12 @@ protected function processWheres()
556556
return $this;
557557
}
558558

559-
public function processWhereEquals($detail)
559+
public function processWhereEquals($detail)
560560
{
561561
$this->processedWheres[$detail['column']] = $detail['value'];
562562
}
563563

564-
public function processWhereNotEquals($detail)
564+
public function processWhereNotEquals($detail)
565565
{
566566
$this->processedWheres[$detail['column']] = '-'.$detail['value'];
567567
}
@@ -601,7 +601,7 @@ protected function processOrders()
601601
return $this->orders;
602602
}
603603

604-
public function addPagination($array)
604+
public function addPagination($array)
605605
{
606606
if($this->perPage != ''){
607607
$array[$this->perPageField] = $this->perPage;
@@ -647,7 +647,7 @@ public function resetOrders()
647647
$this->orders = [];
648648
return $this;
649649
}
650-
650+
651651
protected function processGetResponse($response)
652652
{
653653
return new ResultSet($this, $response, $this->resource);
@@ -672,9 +672,121 @@ protected function processIndividualResponse($response)
672672
}
673673
}
674674

675+
protected function processIndividualPostResponse($response)
676+
{
677+
if($this->getApiDataField() != null){
678+
$data = $response->json()[$this->getApiDataField()];
679+
} else {
680+
$data = $response->json();
681+
}
682+
if(isset($data[0])){
683+
return $this->resource->updateFromBuilder($this->resource->passOnAttributes($data[0]));
684+
} else {
685+
return $this->resource->updateFromBuilder($this->resource->passOnAttributes($data));
686+
}
687+
}
688+
689+
protected function processMultiPostResponse($response)
690+
{
691+
// if($this->getApiDataField() != null){
692+
// $data = $response->json()[$this->getApiDataField()];
693+
// } else {
694+
// $data = $response->json();
695+
// }
696+
// if(isset($data[0])){
697+
// return $this->resource->newFromBuilder($this->resource->passOnAttributes($data[0]));
698+
// } else {
699+
// return $this->resource->newFromBuilder($this->resource->passOnAttributes($data));
700+
// }
701+
}
702+
703+
protected function processIndividualPatchResponse($response)
704+
{
705+
if($this->getApiDataField() != null){
706+
$data = $response->json()[$this->getApiDataField()];
707+
} else {
708+
$data = $response->json();
709+
}
710+
if(isset($data[0])){
711+
return $this->resource->updateFromBuilder($this->resource->passOnAttributes($data[0]));
712+
} else {
713+
return $this->resource->updateFromBuilder($this->resource->passOnAttributes($data));
714+
}
715+
}
716+
717+
protected function processMultiPatchResponse($response)
718+
{
719+
// if($this->getApiDataField() != null){
720+
// $data = $response->json()[$this->getApiDataField()];
721+
// } else {
722+
// $data = $response->json();
723+
// }
724+
// if(isset($data[0])){
725+
// return $this->resource->newFromBuilder($this->resource->passOnAttributes($data[0]));
726+
// } else {
727+
// return $this->resource->newFromBuilder($this->resource->passOnAttributes($data));
728+
// }
729+
}
730+
731+
protected function processIndividualPutResponse($response)
732+
{
733+
if($this->getApiDataField() != null){
734+
$data = $response->json()[$this->getApiDataField()];
735+
} else {
736+
$data = $response->json();
737+
}
738+
if(isset($data[0])){
739+
return $this->resource->updateFromBuilder($this->resource->passOnAttributes($data[0]));
740+
} else {
741+
return $this->resource->updateFromBuilder($this->resource->passOnAttributes($data));
742+
}
743+
}
744+
745+
protected function processMultiPutResponse($response)
746+
{
747+
// if($this->getApiDataField() != null){
748+
// $data = $response->json()[$this->getApiDataField()];
749+
// } else {
750+
// $data = $response->json();
751+
// }
752+
// if(isset($data[0])){
753+
// return $this->resource->newFromBuilder($this->resource->passOnAttributes($data[0]));
754+
// } else {
755+
// return $this->resource->newFromBuilder($this->resource->passOnAttributes($data));
756+
// }
757+
}
758+
759+
protected function processIndividualDeleteResponse($response)
760+
{
761+
if($this->getApiDataField() != null){
762+
$data = $response->json()[$this->getApiDataField()];
763+
} else {
764+
$data = $response->json();
765+
}
766+
if(isset($data[0])){
767+
return $this->resource->updateFromBuilder($this->resource->passOnAttributes($data[0]));
768+
} else {
769+
return $this->resource->updateFromBuilder($this->resource->passOnAttributes($data));
770+
}
771+
}
772+
773+
protected function processMultiDeleteResponse($response)
774+
{
775+
// if($this->getApiDataField() != null){
776+
// $data = $response->json()[$this->getApiDataField()];
777+
// } else {
778+
// $data = $response->json();
779+
// }
780+
// if(isset($data[0])){
781+
// return $this->resource->newFromBuilder($this->resource->passOnAttributes($data[0]));
782+
// } else {
783+
// return $this->resource->newFromBuilder($this->resource->passOnAttributes($data));
784+
// }
785+
}
786+
675787
public function prepareHttpErrorMessage($response)
676788
{
677789
return $response->json()['message'];
678790
}
679791

680-
}
792+
}

0 commit comments

Comments
 (0)