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|null $properties = null ) | ||
| $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 Yiisoft\ActiveRecord\Trait\EventsTrait 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\ActiveRecord\ActiveQueryInterface query ( Yiisoft\ActiveRecord\ActiveRecordInterface|Closure|string|null $modelClass = null ) | ||
| $modelClass | Yiisoft\ActiveRecord\ActiveRecordInterface|Closure|string|null | |
public static function query(ActiveRecordInterface|Closure|string|null $modelClass = null): ActiveQueryInterface
{
$model = match (true) {
$modelClass === null => new static(),
is_string($modelClass) => new $modelClass(),
$modelClass instanceof ActiveRecordInterface => $modelClass,
default => ($modelClass)(),
};
$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|null $properties = null ) | ||
| $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 integer update ( array|null $properties = null ) | ||
| $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 void upsert ( array|null $insertProperties = null, array|boolean $updateProperties = true ) | ||
| $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.