Eloquent9999999Repository.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace UCore\DcatAdmin\Repository;
  3. use Dcat\Admin\Form;
  4. use Dcat\Admin\Grid;
  5. use Illuminate\Database\Eloquent\Model as EloquentModel;
  6. use Illuminate\Pagination\LengthAwarePaginator;
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Support\Facades\DB;
  9. class Eloquent9999999Repository extends \Dcat\Admin\Repositories\EloquentRepository
  10. {
  11. public function getEloquentClass()
  12. {
  13. return $this->eloquentClass;
  14. }
  15. /**
  16. * 更新后
  17. *
  18. * @var array
  19. */
  20. public $update_after;
  21. /**
  22. * 更新前
  23. *
  24. * @var array
  25. */
  26. public $update_before;
  27. /**
  28. * 新增, 生成数据
  29. *
  30. * @var array
  31. */
  32. public $add_after;
  33. /**
  34. * 新增 传入数据
  35. * @var array
  36. */
  37. public $add_before;
  38. /**
  39. * 新增记录.
  40. *
  41. * @param Form $form
  42. * @return mixed
  43. */
  44. public function store(Form $form)
  45. {
  46. $result = null;
  47. DB::transaction(function () use ($form, &$result) {
  48. $model = $this->model();
  49. $updates = $form->updates();
  50. [$relations, $relationKeyMap] = $this->getRelationInputs($model, $updates);
  51. if ($relations) {
  52. $updates = Arr::except($updates, array_keys($relationKeyMap));
  53. }
  54. $this->add_before = $updates;
  55. foreach ($updates as $column => $value) {
  56. $model->setAttribute($column, $value);
  57. }
  58. $result = $model->save();
  59. $this->add_after = $model->toArray();
  60. $this->updateRelation($form, $model, $relations, $relationKeyMap);
  61. });
  62. return $this->model()->getKey();
  63. }
  64. /**
  65. * 更新数据.
  66. *
  67. * @param Form $form
  68. * @return bool
  69. */
  70. public function update(Form $form)
  71. {
  72. /* @var EloquentModel $builder */
  73. $model = $this->model();
  74. if (!$model->getKey()) {
  75. $model->exists = true;
  76. $model->setAttribute($model->getKeyName(), $form->getKey());
  77. }
  78. $result = null;
  79. DB::transaction(function () use ($form, $model, &$result) {
  80. $updates = $form->updates();
  81. [ $relations, $relationKeyMap ] = $this->getRelationInputs($model, $updates);
  82. if ($relations) {
  83. $updates = Arr::except($updates, array_keys($relationKeyMap));
  84. }
  85. foreach ($updates as $column => $value) {
  86. $this->update_before[$column] = $model->getAttribute($column);
  87. /* @var EloquentModel $model */
  88. $model->setAttribute($column, $value);
  89. $this->update_after[$column] = $model->getAttribute($column);
  90. }
  91. $result = $model->update();
  92. $this->updateRelation($form, $model, $relations, $relationKeyMap);
  93. });
  94. return $result;
  95. }
  96. public function get(Grid\Model $model)
  97. {
  98. $this->setSort($model);
  99. $ps= $this->resolvePerPage($model,null);
  100. $model->forPage($ps[3],$ps[0])->addQuery('get', [ $this->getGridColumns() ]);
  101. // dd($model);
  102. $query = $this->newQuery();
  103. if ($this->relations) {
  104. $query->with($this->relations);
  105. }
  106. $total = 999999999;
  107. $data= $model->apply($query, true, $this->getGridColumns());
  108. // dd($ps,request()->path());
  109. $paginator = new LengthAwarePaginator(
  110. $data,
  111. $total,
  112. $ps[0], // 传入每页显示行数
  113. $ps[3], // 传入当前页码
  114. [
  115. 'path'=>'/'.request()->path()
  116. ]
  117. );
  118. return $paginator;
  119. }
  120. }