| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace UCore\DcatAdmin\Repository;
- use Dcat\Admin\Form;
- use Illuminate\Database\Eloquent\Model as EloquentModel;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\DB;
- class EloquentRepository extends \Dcat\Admin\Repositories\EloquentRepository
- {
- public function getEloquentClass()
- {
- return $this->eloquentClass;
- }
- /**
- * 更新后
- *
- * @var array
- */
- public $update_after;
- /**
- * 更新前
- *
- * @var array
- */
- public $update_before;
- /**
- * 新增, 生成数据
- *
- * @var array
- */
- public $add_after;
- /**
- * 新增 传入数据
- * @var array
- */
- public $add_before;
- /**
- * 新增记录.
- *
- * @param Form $form
- * @return mixed
- */
- public function store(Form $form)
- {
- $result = null;
- DB::transaction(function () use ($form, &$result) {
- $model = $this->model();
- $updates = $form->updates();
- [$relations, $relationKeyMap] = $this->getRelationInputs($model, $updates);
- if ($relations) {
- $updates = Arr::except($updates, array_keys($relationKeyMap));
- }
- $this->add_before = $updates;
- foreach ($updates as $column => $value) {
- $model->setAttribute($column, $value);
- }
- $result = $model->save();
- $this->add_after = $model->toArray();
- $this->updateRelation($form, $model, $relations, $relationKeyMap);
- });
- return $this->model()->getKey();
- }
- /**
- * 更新数据.
- *
- * @param Form $form
- * @return bool
- */
- public function update(Form $form)
- {
- /* @var EloquentModel $builder */
- $model = $this->model();
- if (!$model->getKey()) {
- $model->exists = true;
- $model->setAttribute($model->getKeyName(), $form->getKey());
- }
- $result = null;
- DB::transaction(function () use ($form, $model, &$result) {
- $updates = $form->updates();
- [ $relations, $relationKeyMap ] = $this->getRelationInputs($model, $updates);
- if ($relations) {
- $updates = Arr::except($updates, array_keys($relationKeyMap));
- }
- foreach ($updates as $column => $value) {
- $this->update_before[$column] = $model->getAttribute($column);
- /* @var EloquentModel $model */
- $model->setAttribute($column, $value);
- $this->update_after[$column] = $model->getAttribute($column);
- }
- $result = $model->update();
- $this->updateRelation($form, $model, $relations, $relationKeyMap);
- });
- return $result;
- }
- }
|