|
6 | 6 | use BlueFeather\EloquentFileMaker\Database\Query\FMBaseBuilder;
|
7 | 7 | use Illuminate\Database\Eloquent\Builder;
|
8 | 8 | use Illuminate\Database\Eloquent\Model;
|
| 9 | +use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot; |
| 10 | +use Illuminate\Database\Eloquent\Relations\Pivot; |
9 | 11 | use Illuminate\Http\File;
|
10 | 12 | use Illuminate\Support\Collection;
|
11 | 13 | use Illuminate\Support\Str;
|
@@ -335,16 +337,25 @@ protected function performInsert(Builder $query)
|
335 | 337 | // If the model has an incrementing key, we can use the "insertGetId" method on
|
336 | 338 | // the query builder, which will give us back the final inserted ID for this
|
337 | 339 | // table from the database. Not all tables have to be incrementing though.
|
338 |
| - $attributes = $this->getAttributes(); |
| 340 | + $attributes = $this->getAttributesForInsert(); |
| 341 | + |
| 342 | + if ($this->getIncrementing()) { |
| 343 | + $query->createRecord(); |
| 344 | + // perform a refresh after the insert to get the generated primary key / ID and calculated data |
| 345 | + $this->refresh(); |
| 346 | + } |
339 | 347 |
|
340 | 348 | // If the table isn't incrementing we'll simply insert these attributes as they
|
341 | 349 | // are. These attribute arrays must contain an "id" column previously placed
|
342 | 350 | // there by the developer as the manually determined key for these models.
|
343 |
| - if (empty($attributes)) { |
344 |
| - return true; |
| 351 | + else { |
| 352 | + if (empty($attributes)) { |
| 353 | + return true; |
| 354 | + } |
| 355 | + |
| 356 | + $query->createRecord(); |
345 | 357 | }
|
346 | 358 |
|
347 |
| - $query->createRecord(); |
348 | 359 |
|
349 | 360 |
|
350 | 361 | // We will go ahead and set the exists property to true, so that it is set when
|
@@ -493,5 +504,30 @@ public function qualifyColumn($column)
|
493 | 504 | // so just return without the table
|
494 | 505 | return $column;
|
495 | 506 | }
|
| 507 | + /** |
| 508 | + * Reload the current model instance with fresh attributes from the database. |
| 509 | + * |
| 510 | + * @return $this |
| 511 | + */ |
| 512 | + public function refresh() |
| 513 | + { |
| 514 | + // make sure we have a FileMaker internal recordId |
| 515 | + if ($this->recordId === null) { |
| 516 | + return $this; |
| 517 | + } |
| 518 | + |
| 519 | + $this->setRawAttributes( |
| 520 | + $this->findByRecordId($this->recordId)->attributes |
| 521 | + ); |
| 522 | + |
| 523 | + $this->load(collect($this->relations)->reject(function ($relation) { |
| 524 | + return $relation instanceof Pivot |
| 525 | + || (is_object($relation) && in_array(AsPivot::class, class_uses_recursive($relation), true)); |
| 526 | + })->keys()->all()); |
| 527 | + |
| 528 | + $this->syncOriginal(); |
| 529 | + |
| 530 | + return $this; |
| 531 | + } |
496 | 532 |
|
497 | 533 | }
|
0 commit comments