Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
godismyjudge95 opened this issue Feb 12, 2025 · 0 comments
Open

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

godismyjudge95 opened this issue Feb 12, 2025 · 0 comments

Comments

@godismyjudge95
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant