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

Problem When Registering Observers WIth The Attribute Method In Laravel 11 #1335

Open
achatzi78 opened this issue Oct 1, 2024 · 0 comments
Labels

Comments

@achatzi78
Copy link

Describe the bug
When there is a model that has an observer defined with the new attribute method, the logging does not behave as it should.

To Reproduce
Create a model with an observer like this

#[ObservedBy(PackingListObserver::class)]
class PackingList extends Model
{
    use LogsActivity;

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->useLogName('dngr-packing-lists')
            ->logAll()
            ->logExcept([
                'ulid',
                'created_at',
                'updated_at',
            ])
            ->logOnlyDirty()
            ->dontSubmitEmptyLogs();
    }
}

The observer modifies the model after it is created

class PackingListObserver
{
    public function created(PackingList $packing_list): void
    {
        $packing_list->update(['application_sap_id' => "SHP-PL{$packing_list->id}"]);
    }
}

When I create a new packing list, 2 activities are logged, one for created and one for updated (which is correct) but the properties of the activities are exactly the same, like it was created 2 times and only the event name was changed.

Expected behavior
Should have 2 activities but the updated one should only have the modification on the application_sap_id attribute.

Furthermore, I really do not want to register the modification of the application_sap_id attribute. So when I change my classes to this

#[ObservedBy(PackingListObserver::class)]
class PackingList extends Model
{
    use LogsActivity;

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->useLogName('dngr-packing-lists')
            ->logAll()
            ->logExcept([
                'ulid',
                'application_sap_id',
                'created_at',
                'updated_at',
            ])
            ->logOnlyDirty()
            ->dontSubmitEmptyLogs();
    }
}

class PackingListObserver
{
    public function created(PackingList $packing_list): void
    {
        $packing_list->disableLogging()->update(['application_sap_id' => "SHP-PL{$packing_list->id}"]);
        $packing_list->enableLogging();
    }
}

Nothing is registered.

I believe it has something to do with the order the observers are registered to the model, because if I use the old method, everything works fine

class PackingList extends Model
{
    use LogsActivity;

    protected static function boot()
    {
        parent::boot();

        parent::observe(PackingListObserver::class);
    }

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->useLogName('dngr-packing-lists')
            ->logAll()
            ->logExcept([
                'ulid',
                'application_sap_id',
                'created_at',
                'updated_at',
            ])
            ->logOnlyDirty()
            ->dontSubmitEmptyLogs();
    }
}

class PackingListObserver
{
    public function created(PackingList $packing_list): void
    {
        $packing_list->disableLogging()->update(['application_sap_id' => "SHP-PL{$packing_list->id}"]);
    }
}

Versions (please complete the following information)

  • PHP: 8.2
  • Database: MySql 8.2
  • Laravel: 11.23.2
  • Package: 4.8.0

Additional context
I don't have an issue working with the old method of registering observers, but in case it is deprecated there will be a problem using this package.

@achatzi78 achatzi78 added the bug label Oct 1, 2024
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

1 participant