Skip to content

Commit 0310942

Browse files
committed
fixes for inserting dates, timestamps, and custom types which was causing formatting errors
1 parent 53e1013 commit 0310942

File tree

2 files changed

+50
-41
lines changed

2 files changed

+50
-41
lines changed

src/Database/Eloquent/Concerns/FMHasAttributes.php

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,58 @@
22

33
namespace GearboxSolutions\EloquentFileMaker\Database\Eloquent\Concerns;
44

5+
use DateTime;
56
use Illuminate\Database\Eloquent\Concerns\HasAttributes;
7+
use Illuminate\Support\Arr;
68

79
trait FMHasAttributes
810
{
9-
use HasAttributes;
1011

1112
/**
12-
* Determine whether a value is Date / DateTime castable for inbound manipulation.
13+
* Set a given attribute on the model.
1314
*
14-
* @param string $key
15-
* @return bool
15+
* @param string $key
16+
* @param mixed $value
17+
* @return mixed
1618
*/
17-
protected function isDateCastable($key)
19+
public function setAttribute($key, $value)
1820
{
19-
// We need to also cast timestamps as
20-
return $this->hasCast($key, ['date', 'datetime', 'immutable_date', 'immutable_datetime', 'timestamp', 'custom_datetime']);
21+
parent::setAttribute($key, $value);
22+
23+
$value = $this->attributes[$key];
24+
25+
// Check if we still have a DateTime object due to custom formatting and convert it to a string to write to FM.
26+
// Normally the SQL grammar would handle converting DateTime objects and SQL doesn't care about extra time data,
27+
// but FileMaker does, so we have to convert at this point and strip out times.
28+
//
29+
// We could convert the DateTime to a string at the time when we're preparing the API call, but at that point
30+
// we won't be in the model and won't have access to the cast type to determine if we should strip out the
31+
// time data.
32+
33+
if ($value instanceof DateTime) {
34+
$value = $value->format($this->dateFormat);
35+
}
36+
// When writing dates the regular datetime format won't work, so we have to get JUST the date value
37+
// check the key's cast to see if it is cast to a date or custom date:format
38+
$castType = $this->getCasts()[$key] ?? null;
39+
$isDate = $castType == "date" || str_starts_with($castType, 'date:');
40+
if ($isDate) {
41+
$value = Arr::first(explode(' ', $value));
42+
}
43+
44+
// FileMaker can't handle true and false, so we need to change to 1 and 0
45+
if (is_bool($value)) {
46+
$value = $value ? 1 : 0;
47+
}
48+
49+
// FileMaker can't handle null, so change it to ''
50+
if (is_null($value)) {
51+
$value = '';
52+
}
53+
54+
$this->attributes[$key] = $value;
55+
56+
return $this;
2157
}
2258

2359
}

src/Database/Eloquent/FMModel.php

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Illuminate\Database\Eloquent\Relations\Pivot;
1414
use Illuminate\Http\File;
1515
use Illuminate\Http\UploadedFile;
16-
use Illuminate\Support\Arr;
1716
use Illuminate\Support\Collection;
1817
use Illuminate\Support\Str;
1918

@@ -74,6 +73,13 @@ abstract class FMModel extends Model
7473
*/
7574
protected $keyType = 'string';
7675

76+
/**
77+
* The date format to use when writing to the database.
78+
*
79+
* @var string
80+
*/
81+
protected $dateFormat = 'm/j/Y H:i:s';
82+
7783

7884
public function __construct(array $attributes = [])
7985
{
@@ -472,39 +478,6 @@ public function getTable()
472478
return $this->table ?? $this->layout ?? Str::snake(Str::pluralStudly(class_basename($this)));
473479
}
474480

475-
/**
476-
* Set a given attribute on the model.
477-
*
478-
* @param string $key
479-
* @param mixed $value
480-
* @return mixed
481-
*/
482-
public function setAttribute($key, $value)
483-
{
484-
parent::setAttribute($key, $value);
485-
486-
$value = $this->attributes[$key];
487-
488-
// When writing dates the regular datetime format won't work, so we have to get JUST the date value
489-
if ($this->isDateAttribute($key) && ($this->hasCast($key, ['date'] || $this->hasCast($key, ['custom_date']) || $this->hasCast($key, ['immutable_date'])))) {
490-
$value = Arr::first(explode(' ', $value));
491-
}
492-
493-
// FileMaker can't handle true and false, so we need to change to 1 and 0
494-
if (is_bool($value)) {
495-
$value = $value ? 1 : 0;
496-
}
497-
498-
// FileMaker can't handle null, so change it to ''
499-
if (is_null($value)) {
500-
$value = '';
501-
}
502-
503-
$this->attributes[$key] = $value;
504-
505-
return $this;
506-
}
507-
508481

509482
/**
510483
* Qualify the given column name by the model's table.

0 commit comments

Comments
 (0)