Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

模型增加extraSave方法,支持触发模型写入数据事件(同replace) #472

Open
wants to merge 1 commit into
base: 3.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
*/
private $replace = false;

/**
* 保存数据额外参数.
*
* @var string
*/
private $extraSave = '';

/**
* 数据表后缀
*
Expand Down Expand Up @@ -468,6 +475,20 @@ public function replace(bool $replace = true)
return $this;
}

/**
* 设置保存数据的额外参数.
*
* @param string $extraSave 额外信息
*
* @return $this
*/
public function extraSave(string $extraSave)
{
$this->extraSave = $extraSave;

return $this;
}

/**
* 刷新模型数据.
*
Expand Down Expand Up @@ -656,6 +677,7 @@ protected function updateData(): bool
->cache(true)
->setOption('key', $this->key)
->field($allowFields)
->extra($this->extraSave)
->update($data);

$this->checkResult($result);
Expand Down Expand Up @@ -710,6 +732,7 @@ protected function insertData(string $sequence = null): bool
$result = $db->strict(false)
->field($allowFields)
->replace($this->replace)
->extra($this->extraSave)
->sequence($sequence)
->insert($data, true);

Expand Down Expand Up @@ -848,10 +871,11 @@ public function delete(): bool
* @param array $allowField 允许字段
* @param bool $replace 使用Replace
* @param string $suffix 数据表后缀
* @param string $extraSave 保存数据额外参数
*
* @return static
*/
public static function create(array $data, array $allowField = [], bool $replace = false, string $suffix = ''): Model
public static function create(array $data, array $allowField = [], bool $replace = false, string $suffix = '', string $extraSave = ''): Model
{
$model = new static();

Expand All @@ -863,7 +887,7 @@ public static function create(array $data, array $allowField = [], bool $replace
$model->setSuffix($suffix);
}

$model->replace($replace)->save($data);
$model->replace($replace)->extraSave($extraSave)->save($data);

return $model;
}
Expand Down