Модельные события: saving/creating/updating/deleting/restoring и их пары после
This commit is contained in:
1 parent
0835ea081c
commit
7d093ad29c
5 files changed
+229
-2
No files matched your search
@@ -11,6 +11,7 @@ use Rsgrinko\Proton\Database\Model\Relations\BelongsToMany;
|
||||
use Rsgrinko\Proton\Database\Model\Relations\HasMany;
|
||||
use Rsgrinko\Proton\Database\Model\Relations\HasOne;
|
||||
use Rsgrinko\Proton\Database\Model\Relations\Relation;
|
||||
use Rsgrinko\Proton\Events\Events;
|
||||
use Rsgrinko\Proton\Support\ProtonException;
|
||||
use Rsgrinko\Proton\Support\Str;
|
||||
|
||||
@@ -321,6 +322,10 @@ abstract class Model implements JsonSerializable
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->before('saving') || $this->before('updating', ['changes' => $changes])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->timestamps && $this->hasColumn(self::UPDATED_AT)) {
|
||||
$changes[self::UPDATED_AT] = $this->attributes[self::UPDATED_AT] = $now;
|
||||
}
|
||||
@@ -329,9 +334,16 @@ abstract class Model implements JsonSerializable
|
||||
|
||||
$this->original = $this->attributes;
|
||||
|
||||
$this->after('updated');
|
||||
$this->after('saved');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->before('saving') || $this->before('creating')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->timestamps) {
|
||||
foreach ([self::CREATED_AT, self::UPDATED_AT] as $column) {
|
||||
if (!isset($this->attributes[$column]) && $this->hasColumn($column)) {
|
||||
@@ -349,6 +361,9 @@ abstract class Model implements JsonSerializable
|
||||
$this->original = $this->attributes;
|
||||
$this->exists = true;
|
||||
|
||||
$this->after('created');
|
||||
$this->after('saved');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -371,6 +386,10 @@ abstract class Model implements JsonSerializable
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->before('deleting')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->softDelete) {
|
||||
$this->attributes[self::DELETED_AT] = date('Y-m-d H:i:s');
|
||||
|
||||
@@ -380,6 +399,8 @@ abstract class Model implements JsonSerializable
|
||||
[static::$primaryKey => $this->key()]
|
||||
);
|
||||
|
||||
$this->after('deleted');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -387,6 +408,8 @@ abstract class Model implements JsonSerializable
|
||||
|
||||
$this->exists = false;
|
||||
|
||||
$this->after('deleted');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -399,10 +422,16 @@ abstract class Model implements JsonSerializable
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->before('deleting', ['force' => true])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->connection()->delete($this->table(), [static::$primaryKey => $this->key()]);
|
||||
|
||||
$this->exists = false;
|
||||
|
||||
$this->after('deleted', ['force' => true]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -415,6 +444,10 @@ abstract class Model implements JsonSerializable
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->before('restoring')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->attributes[self::DELETED_AT] = null;
|
||||
|
||||
$this->connection()->update(
|
||||
@@ -423,6 +456,8 @@ abstract class Model implements JsonSerializable
|
||||
[static::$primaryKey => $this->key()]
|
||||
);
|
||||
|
||||
$this->after('restored');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -718,6 +753,31 @@ abstract class Model implements JsonSerializable
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Событие "до": model.saving, model.creating, model.updating, model.deleting,
|
||||
* model.restoring. Слушатель узнаёт модель по `$payload['model'] instanceof …`
|
||||
* и может отменить операцию, вернув false, — как `mail.sending` у почты.
|
||||
*
|
||||
* @param array<string, mixed> $extra
|
||||
*/
|
||||
private function before(string $event, array $extra = []): bool
|
||||
{
|
||||
$results = Events::fire('model.' . $event, ['model' => $this] + $extra);
|
||||
|
||||
return in_array(false, $results, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Событие "после": model.created, model.updated, model.saved, model.deleted,
|
||||
* model.restored. Отменить уже нельзя — запись изменилась.
|
||||
*
|
||||
* @param array<string, mixed> $extra
|
||||
*/
|
||||
private function after(string $event, array $extra = []): void
|
||||
{
|
||||
Events::fire('model.' . $event, ['model' => $this] + $extra);
|
||||
}
|
||||
|
||||
/**
|
||||
* Есть ли колонка в таблице. Ответ на процесс один: спрашивать схему на
|
||||
* каждое сохранение незачем.
|
||||
|
||||
Reference in new issue
Block a user