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 integer delete ( ) |
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 void insert ( ?array $properties = null ) | ||
| $properties | ?array | |
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 static populateRecord ( array|object $data ) | ||
| $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 Yiisoft\ | ||
| $modelClass | Yiisoft\ |
|
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 void save ( ?array $properties = null ) | ||
| $properties | ?array | |
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 integer update ( ?array $properties = null ) | ||
| $properties | ?array | |
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 void upsert ( ?array $insertProperties = null, array|boolean $updateProperties = true ) | ||
| $insertProperties | ?array | |
| $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));
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.