cjmellor / approval Goto Github PK
View Code? Open in Web Editor NEWApprove new Model data before it is persisted
License: MIT License
Approve new Model data before it is persisted
License: MIT License
When a model has a morphMap alias (see docs), approving the Approval results in the error Error: Class "model_name" not found.
When creating the class, Relation::getMorphedModel($approvalable_type)
should be called instead of directly instancing the database table value.
This is done in Laravel 10
Let's say I've got the following Model:
class Post extends Model
{
use MustBeApproved;
protected $fillable = [
"config",
];
protected $casts = [
"config" => "json",
];
}
Now let's assume we're making different API calls where we change this Model and approve the modifications, the creation works fine
$post = new Post();
$post->config = ['checked' => false];
$post->save();
When updating the post (which needs an approval)
$post = Post::find([id]);
$post->config = ['checked' => true];
$post->save();
I get the following approvals, which I already approved in this image, but you can already see the discrepancy in data
after rolling back this change I get the following approvals
Now after approving the rolled-back approval (which is pending as well) I get the error
"message": "json_decode(): Argument https://github.com/cjmellor/approval/pull/1 ($json) must be of type string, array given", "exception": "TypeError", "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Casts/Json.php"
When using Approval::find(3)->approve(). It's updating 1,2,3.
As said in the title $approval->reject()
when called proceeds to reject all pending models instead of just the one queried
Sample code:
$approvals = Approval::pending()->get();
count($approvals); // 10
$approval->first()->reject();
Approval::pending()->count(); // 0
I am talking about this https://github.com/barryvdh/laravel-ide-helper. A must use package if you develop with vscode.
If you run
php artisan ide-helper:generate -q
You will get
Exception: Target class [approval] does not exist.
Skipping \Cjmellor\Approval\Facades\Approval.
Exception: Target class [approval] does not exist.
Skipping \Cjmellor\Approval\Facades\Approval.
In my model I have an attribute which is casts to array:
protected $casts = [ 'applied_person_data' => 'array' ];
After approving a change, the json object which will be written back to the model has added slashes, which breaks the models behavior.
# 1
\App\Models\Process::withoutApproval()->create('name' => 'xyz', 'description' => 'abc') // Method cannot be called statically.
# 2
$process = new \App\Models\Process();
$process->withoutApproval()->create([ 'name' => 'xyz', 'description' => 'abc']);
This creates an approval record with a pending status instead of directly writing to the processes
table.
In your approvalModelExists
method, you are checking for an exact match for new_data
and original_data
via json_encode
. I'm not sure how .sqlite formats data in json columns, but it appears to be the same as json_encode
, since it works for your tests. In my local database I'm running MySQL 8, and it puts spaces between the key and value pairs in those columns, so the exact match in your query isn't working because of slight formatting differences between php and mysql, and thus duplicates will always be created.
I've tried the following and it seems to work:
return Approval::where('state', ApprovalStatus::Pending)
->whereJsonContains('new_data', $model->getDirty())
->whereJsonContains('original_data', $model->getOriginalMatchingChanges())
->exists();
If you want, I can submit a PR using the whereJsonContains
query builder method.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.