Skip to content

Json Cast #100

@demobiel

Description

@demobiel

Hi,

if a column is cast to json, and you want to do a partial update with the notation "myJsonField->firstname" it does not work, as the custom setAttribute still wants to deal with it.

Simply adding a return statement when you detect a "->" fixes the problem, the call to the parent::setAttribute() already deals with it.

  public function setAttribute($key, $value)
    {
        parent::setAttribute($key, $value);
        
        //don't deal with json casting, already done in the call above
        if(str_contains($key,'->')) return;

        $value = $this->attributes[$key];

        // Check if we still have a DateTime object due to custom formatting and convert it to a string to write to FM.
        // Normally the SQL grammar would handle converting DateTime objects and SQL doesn't care about extra time data,
        // but FileMaker does, so we have to convert at this point and strip out times.
        //
        // We could convert the DateTime to a string at the time when we're preparing the API call, but at that point
        // we won't be in the model and won't have access to the cast type to determine if we should strip out the
        // time data.

        if ($value instanceof DateTime) {
            $value = $value->format($this->dateFormat);
        }
        // When writing dates the regular datetime format won't work, so we have to get JUST the date value
        // check the key's cast to see if it is cast to a date or custom date:format
        $castType = $this->getCasts()[$key] ?? '';
        $isDate = $castType == 'date' || str_starts_with($castType, 'date:');
        if ($isDate && ($value !== null)) {
            $value = Arr::first(explode(' ', $value));
        }

        // FileMaker can't handle true and false, so we need to change to 1 and 0
        if (is_bool($value)) {
            $value = $value ? 1 : 0;
        }

        $this->attributes[$key] = $value;

        return $this;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions