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

Duplicate entries for every action #896

Closed
K-Neu-Projects opened this issue Jan 9, 2024 · 8 comments
Closed

Duplicate entries for every action #896

K-Neu-Projects opened this issue Jan 9, 2024 · 8 comments
Labels

Comments

@K-Neu-Projects
Copy link

Q A
Bug? yes
New Feature? no
Framework Laravel
Framework version 8.0
Package version 13.6
PHP version 8.0

Actual Behaviour

The audits table creates two identical entries for every action.

Expected Behaviour

I'd expect a single entry for every action.

Steps to Reproduce

I'm using the default configuration for the most part except the table name. That I changed to 'auto_audits'.
Also since its Laravel 8 I did not explicitly register the OwenIt\Auditing\AuditingServiceProvider::class.

use OwenIt\Auditing\Auditable as AuditingAuditable;
use OwenIt\Auditing\Contracts\Auditable;

class MyModel extends Model implements Auditable
{
    use CamelCaseAttributes;
    use AuditingAuditable;
...

I checked the query output using DB::enableQueryLog and DB::getQueryLog which shows 1 Update query and 2 inserts into the audits table with the same values.

Possible Solutions

Does the autoload somehow cause the event to be fired twice? Thats my only idea based on my findings.

@angeljqv
Copy link
Contributor

angeljqv commented Jan 9, 2024

Seems like you are registering OwenIt\Auditing\AuditingServiceProvider::class twice

@K-Neu-Projects
Copy link
Author

But how?
Im not registering it manually at all.
I tried explicitly listing it in the app/config.php providers but the result is the same.

@angeljqv
Copy link
Contributor

Im not registering it manually at all.

It is automatic

I tried explicitly listing it in the app/config.php providers

Worst

@K-Neu-Projects
Copy link
Author

Thats why I didnt register it manually at first just to check if it would resolve the issue which it didnt.

So the automatic discovery somehow registers it twice if thats the issue.

@angeljqv
Copy link
Contributor

angeljqv commented Jan 10, 2024

It doesn't seem like a problem with this package, it seems like a problem on your implementation,
try a clean Laravel installation
try uninstalling and reinstalling the package,
upload an example repository with the bug/failing test so someone can review it
I have three applications with this package without problems

@K-Neu-Projects
Copy link
Author

Will do so, thanks for the input

@K-Neu-Projects
Copy link
Author

So, I tried some stuff,

  • Updated to Laravel 9 -> no change
  • Reinstalled the lib -> no change
  • setup test project -> works fine

Even after I removed the registration of the AuditingEventServiceProvider in the package, I still got 2 entries. Still puzzles me a bit since I expected none after that.

But yeah, as mentioned, some issue with the project so this can be closed.
thanks again for the input though!

@K-Neu-Projects
Copy link
Author

Alright after debugging the issue for quite some time I noticed that the Auditing event was fired twice for every send.

The issue was the booted method in the model:

class MyModel extends Model implements Auditable
{
    use CamelCaseAttributes;
    use AuditingAuditable;

    protected static function booted()
    {
        static::addGlobalScope(new CustomScope());

        parent::booted(); // this line is the issue
    }
...

The Auditable trait was initialized twice since the laravel Model which is my base class recursivly initializes the traits on the class in its booted() method. This causes 2 auditing events and therefor 2 db entries.

Fix was to remove the parent::booted(); line completly since the constructor of the laravel Model::class is enough to set everything up.

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

No branches or pull requests

3 participants