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

Issue creating an approval when it has a field cast as "array" type #68

Open
anchovie91471 opened this issue Sep 4, 2024 · 2 comments

Comments

@anchovie91471
Copy link

anchovie91471 commented Sep 4, 2024

I have a model with a field that stores data in a JSON db column. I am casting that data as "array" on the model.

protected $casts = [ 'types' => 'array', ];

When creating a new approval on the model, the data from that field is escaped before it's written to the approval table. When approving the model with:

Approval::where('id', 1)->approve();

The escaped data is written to the intended model's table but isn't cast as an array by Laravel because of the extra characters on the string.

I think what's needed is at line 32 in MustBeApproved.php.

change:
if (isset($model->casts[$key]) && $model->casts[$key] === 'json') {

to
if (isset($model->casts[$key]) && ($model->casts[$key] === 'json' || $model->casts[$key] === 'array')) {

I made the above change, created a new model, that saved an approval, and then approved the model. It works. I'm happy to submit a PR for the change if needed.

@cjmellor
Copy link
Owner

cjmellor commented Sep 5, 2024

Yeah you can do a PR. Please write a passing test also and make sure it's all working together.

@anchovie91471
Copy link
Author

Looking over the existing tests, it appears there is already a test for a model with an array cast. The test description is "approve a attribute of the type Array".

  1. It creates a table with a json column ('data')
  2. It creates a model with MustBeApproved and casts 'data' as an array
  3. It creates a new model and fills 'data' with an array
  4. Does the standard database checks
  5. Model is approve with ->approve()
  6. Checks database again after approval
  7. Fetches the model and checks that it is an array

This seems exactly the test that's needed for my code edit, but I can't see why that test has passed before based on what is being written to my model.

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

2 participants