| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace UCore\DcatAdmin\Repository;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Illuminate\Database\Eloquent\Model as EloquentModel;
- use Illuminate\Pagination\LengthAwarePaginator;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\DB;
- class Eloquent9999999Repository 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;
- }
- public function get(Grid\Model $model)
- {
- $this->setSort($model);
- $ps= $this->resolvePerPage($model,null);
- $model->forPage($ps[3],$ps[0])->addQuery('get', [ $this->getGridColumns() ]);
- // dd($model);
- $query = $this->newQuery();
- if ($this->relations) {
- $query->with($this->relations);
- }
- $total = 999999999;
- $data= $model->apply($query, true, $this->getGridColumns());
- // dd($ps,request()->path());
- $paginator = new LengthAwarePaginator(
- $data,
- $total,
- $ps[0], // 传入每页显示行数
- $ps[3], // 传入当前页码
- [
- 'path'=>'/'.request()->path()
- ]
- );
- return $paginator;
- }
- }
|