Trait Yiisoft\ActiveRecord\Trait\EventsTrait
Trait to implement event dispatching for ActiveRecord.
See also:
- Yiisoft\ActiveRecord\ActiveRecordInterface::delete()
- Yiisoft\ActiveRecord\ActiveRecordInterface::insert()
- Yiisoft\ActiveRecord\ActiveRecordInterface::populateRecord()
- Yiisoft\ActiveRecord\ActiveRecordInterface::query()
- Yiisoft\ActiveRecord\ActiveRecordInterface::save()
- Yiisoft\ActiveRecord\ActiveRecordInterface::update()
- Yiisoft\ActiveRecord\ActiveRecordInterface::upsert()
Public Methods
Method Details
| public delete( ): integer |
public function delete(): int
{
$eventDispatcher = EventDispatcherProvider::get(static::class);
$eventDispatcher->dispatch($event = new BeforeDelete($this));
if ($event->isDefaultPrevented()) {
return $event->getReturnValue() ?? 0;
}
$result = parent::delete();
$eventDispatcher->dispatch(new AfterDelete($this, $result));
return $result;
}
| public insert( array|null $properties = null ): void | ||
| $properties | array|null | |
public function insert(?array $properties = null): void
{
$eventDispatcher = EventDispatcherProvider::get(static::class);
$eventDispatcher->dispatch($event = new BeforeInsert($this, $properties));
if ($event->isDefaultPrevented()) {
return;
}
parent::insert($properties);
$eventDispatcher->dispatch(new AfterInsert($this));
}
| public populateRecord( array|object $data ): Yiisoft\ActiveRecord\Trait\EventsTrait | ||
| $data | array|object | |
public function populateRecord(array|object $data): static
{
$eventDispatcher = EventDispatcherProvider::get(static::class);
$eventDispatcher->dispatch($event = new BeforePopulate($this, $data));
if ($event->isDefaultPrevented()) {
return $this;
}
parent::populateRecord($data);
$eventDispatcher->dispatch(new AfterPopulate($this, $data));
return $this;
}
| public static query( Yiisoft\ActiveRecord\ActiveRecordInterface|string|null $modelClass = null ): Yiisoft\ActiveRecord\ActiveQueryInterface | ||
| $modelClass | Yiisoft\ActiveRecord\ActiveRecordInterface|string|null | |
public static function query(ActiveRecordInterface|string|null $modelClass = null): ActiveQueryInterface
{
$model = $modelClass instanceof ActiveRecordInterface
? $modelClass
: new ($modelClass ?? static::class)();
$eventDispatcher = EventDispatcherProvider::get($model::class);
$eventDispatcher->dispatch($event = new BeforeCreateQuery($model));
if ($event->isDefaultPrevented()) {
return $event->getReturnValue();
}
$query = parent::query($model);
$eventDispatcher->dispatch(new AfterCreateQuery($model, $query));
return $query;
}
| public save( array|null $properties = null ): void | ||
| $properties | array|null | |
public function save(?array $properties = null): void
{
$eventDispatcher = EventDispatcherProvider::get(static::class);
$eventDispatcher->dispatch($event = new BeforeSave($this, $properties));
if ($event->isDefaultPrevented()) {
return;
}
parent::save($properties);
$eventDispatcher->dispatch(new AfterSave($this));
}
| public update( array|null $properties = null ): integer | ||
| $properties | array|null | |
public function update(?array $properties = null): int
{
$eventDispatcher = EventDispatcherProvider::get(static::class);
$eventDispatcher->dispatch($event = new BeforeUpdate($this, $properties));
if ($event->isDefaultPrevented()) {
return $event->getReturnValue() ?? 0;
}
$result = parent::update($properties);
$eventDispatcher->dispatch(new AfterUpdate($this, $result));
return $result;
}
| public upsert( array|null $insertProperties = null, array|boolean $updateProperties = true ): void | ||
| $insertProperties | array|null | |
| $updateProperties | array|boolean | |
public function upsert(?array $insertProperties = null, array|bool $updateProperties = true): void
{
$eventDispatcher = EventDispatcherProvider::get(static::class);
$eventDispatcher->dispatch($event = new BeforeUpsert($this, $insertProperties, $updateProperties));
if ($event->isDefaultPrevented()) {
return;
}
parent::upsert($insertProperties, $updateProperties);
$eventDispatcher->dispatch(new AfterUpsert($this));
}
Signup or Login in order to comment.