Skip to content

[2.x] Array/Json cast column is double encoded #171

@godismyjudge95

Description

@godismyjudge95

Not sure how to explain this but when you have a array/json-like cast column like so:

protected function casts()
{
	return ['summary' => 'array'];
}

And then you save data to it:

$model = new Model();
$model->summary = [['test' => 'test']];
$model->save();

The data in the content directory is correct and if you check the orbit sqlite cache it is correct. But then at some point orbit decides to refresh the data from the flat file. Something in that process causes the array/json attribute to be double json encoded.

For example before the refresh the flat file would look like:

summary: '[{"test":"test"}]'

And the sqlite database column would look like:

summary
[{"test":"test"}]

But after Orbit does it's refresh, the database column looks like:

summary
"[{\"test\":\"test\"}]"

Which means somewhere along the line the data got re-encoded as json. I am guessing it has something to do with these lines: https://github.com/ryangjchandler/orbit/blob/2.x/src/Actions/MaybeRefreshDatabaseContent.php#L56-L62

My guess would be that since the data is loaded directly from the flat file and the flat file stores json columns as strings, then when it refreshes the database like this it must be feeding the already json encoded data into Eloquent's cast system again only for Eloquent to re-encode it.

Update:
Before I even submitted this, I had a thought that maybe the ->setAttribute() call in the loop https://github.com/ryangjchandler/orbit/blob/2.x/src/Actions/MaybeRefreshDatabaseContent.php#L56-L58 is the problem. I did a bit of research and found there is another method ->setRawAttributes(). Removing the whole loop and using that completely solved my problem in this case. But it may cause other side effects I am not aware of? If I get time I'll write a few tests and see if that is the solution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions