cloudcake / laravel-approval

Attach an approval process to any model, approve and disapprove from any other model, for Laravel.
MIT License
143 stars 15 forks source link

Approving a newly created record: Call to a member function applyModificationChanges() #9

Closed huge10 closed 4 years ago

huge10 commented 4 years ago

When a model with RequiredApproval is freshly created, a Modification is created correctly. When I approve it using:

$user->approve($modification);

and since it has 0 remaining approvers left it tries to apply the changes and I get:

Call to a member function applyModificationChanges() on null

which makes sense as here:

$modification->modifiable->applyModificationChanges($modification, true);

I guess there is no 'modifiable' (as the record does not exist yet / has no idea).

Am I missing something here? Love your work, works perfect for changes but I can't seem to get it to work for creations.

stephenlake commented 4 years ago

Thanks for the report, I'll unfortunately only be able to look into this later this week.

huge10 commented 4 years ago

I currently do this dirty hack in ApprovesChanges trait (mind you, novice developer):

            if ($modification->approversRemaining == 0) {
                if($modification->modifiable_id === null) {
                    $polymorphicModel = new $modification->modifiable_type();
                    $polymorphicModel->applyModificationChanges($modification, true);
                } else {
                    $modification->modifiable->applyModificationChanges($modification, true);
                }
            }