andersao / andersao/l5-repository
After and before hooks ?
- 主要语言
- PHP
- 星标
- 4.2k
- 派生
- 880
- PR 合并指标
- 30 天内没有已合并 PR
描述
Hello,
Maybe it is a good idea to add the after / before hooks for delete / create / update ?
Something like :
``` php
public function update(array $attributes, $id)
{
$this->applyScope();
if ( !is_null($this->validator) ) {
$this->validator->with($attributes)
->setId($id)
->passesOrFail( ValidatorInterface::RULE_UPDATE );
}
$_skipPresenter = $this->skipPresenter;
$this->skipPresenter(true);
$model = $this->model->findOrFail($id);
// before
$methodBefore = 'beforeUpdate';
if (method_exists($this, $methodBefore)) {
$result = call_user_func_array([$this, $methodBefore], [$model, $attributes]);
if ($result === false) return $result;
}
$model->fill($attributes);
$model->save();
// after
$methodAfter = 'afterUpdate';
if (method_exists($this, $methodAfter)) {
$result = call_user_func_array([$this, $methodAfter], [$model, $attributes]);
if ($result === false) return $result;
}
$this->skipPresenter($_skipPresenter);
$this->resetModel();
event(new RepositoryEntityUpdated($this, $model));
return $this->parserResult($model);
}
```
Then implement the required method in the repository :
``` php
class UserRepository extends BaseRepository
{
/**
* Specify Model class name
* @return string
*/
function model()
{
return \TrackerApi\Models\User::class;
}
public function beforeUpdate($model, $attributes)
{
// Before
}
public function afterUpdate($model, $attributes)
{
// after
}
}
```
What do you think ?
贡献指南
评估
这个 Issue 还没有评估数据。