friendsofhyperf/model-observer

The model observer component for Hyperf.

v3.1.41 2024-09-25 00:28 UTC

README

Latest Stable Version Total Downloads License

The model observer component for Hyperf.

Installation

composer require friendsofhyperf/model-observer

Usage

  • Generator command
php bin/hyperf.php gen:observer TestObserver --model="App\\Model\\User"
  • Single binding
namespace App\Observer;

use App\Model\User;
use FriendsOfHyperf\ModelObserver\Annotation\Observer;

#[Observer(model: User::class)]
class FooObserver
{
    public function creating(User $model)
    {
        // do sth...
    }

    public function created(User $model)
    {
        // do sth...
    }

    // another events
}
  • Multiple binding
namespace App\Observer;

use App\Model\Post;
use App\Model\User;
use FriendsOfHyperf\ModelObserver\Annotation\Observer;

#[Observer(model: [User::class, Post::class])]
class FooObserver
{
    public function creating($model)
    {
        // do sth...
    }

    public function created($model)
    {
        // do sth...
    }

    // another events
}
  • Binding on model
namespace App\Model;

use App\Observer\FooObserver;

#[ObservedBy(FooObserver::class)]
class User extends Model
{
    // ...
}

Methods

  • booting
  • booted
  • retrieved
  • creating
  • created
  • updating
  • updated
  • saving
  • saved
  • restoring
  • restored
  • deleting
  • deleted
  • forceDeleted

Contact

License

MIT