From 4f275143aa9a5610b3d37f45bdc85062ed61fcdc Mon Sep 17 00:00:00 2001 From: pxpm Date: Tue, 23 Jul 2024 17:16:32 +0100 Subject: [PATCH 1/2] use 4 queries instead of upsert --- src/app/Library/CrudPanel/Traits/Reorder.php | 34 ++++++++++++-------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/app/Library/CrudPanel/Traits/Reorder.php b/src/app/Library/CrudPanel/Traits/Reorder.php index 1b235772c6..ffcb038218 100644 --- a/src/app/Library/CrudPanel/Traits/Reorder.php +++ b/src/app/Library/CrudPanel/Traits/Reorder.php @@ -2,6 +2,8 @@ namespace Backpack\CRUD\app\Library\CrudPanel\Traits; +use Illuminate\Support\Facades\DB; + /** * Properties and methods for the Reorder operation. */ @@ -30,24 +32,30 @@ public function updateTreeOrder($request) $item['parent_id'] = empty($item['parent_id']) ? null : (int) $item['parent_id']; $item['depth'] = empty($item['depth']) ? null : (int) $item['depth']; $item['lft'] = empty($item['left']) ? null : (int) $item['left']; - $item['rgt'] = empty($item['right']) ? null : (int) $item['right']; - // we are not touching those two columns on update, but we need them to be present - // for the upsert query to work. they will just be ignored and not touched. - $item['name'] = ''; - $item['slug'] = ''; - + $item['rgt'] = empty($item['right']) ? null : (int) $item['right']; // unset mapped items properties. unset($item['item_id'], $item['left'], $item['right']); return $item; })->toArray(); - - $this->model->upsert( - $reorderItems, - $primaryKey, - ['parent_id', 'depth', 'lft', 'rgt'] - ); - + + DB::transaction(function () use ($reorderItems, $primaryKey, $itemKeys) { + $reorderItemsBindString = implode(',', array_fill(0, count($reorderItems), '?')); + foreach(['parent_id', 'depth', 'lft', 'rgt'] as $column) { + $query = ''; + $bindings = []; + $query .= "UPDATE {$this->model->getTable()} SET {$column} = CASE "; + foreach ($reorderItems as $item) { + $query .= "WHEN {$primaryKey} = ? THEN ? "; + $bindings[] = $item[$primaryKey]; + $bindings[] = $item[$column]; + } + array_push($bindings, ...$itemKeys->toArray()); + $query .= "ELSE {$column} END WHERE {$primaryKey} IN ({$reorderItemsBindString})"; + DB::statement($query, $bindings); + } + }); + return count($reorderItems); } From 994e2b5cc04dc89b6ee5d4c97b30d198e7269f11 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 23 Jul 2024 16:16:48 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI [ci skip] [skip ci] --- src/app/Library/CrudPanel/Traits/Reorder.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/app/Library/CrudPanel/Traits/Reorder.php b/src/app/Library/CrudPanel/Traits/Reorder.php index ffcb038218..d22799827f 100644 --- a/src/app/Library/CrudPanel/Traits/Reorder.php +++ b/src/app/Library/CrudPanel/Traits/Reorder.php @@ -32,18 +32,18 @@ public function updateTreeOrder($request) $item['parent_id'] = empty($item['parent_id']) ? null : (int) $item['parent_id']; $item['depth'] = empty($item['depth']) ? null : (int) $item['depth']; $item['lft'] = empty($item['left']) ? null : (int) $item['left']; - $item['rgt'] = empty($item['right']) ? null : (int) $item['right']; + $item['rgt'] = empty($item['right']) ? null : (int) $item['right']; // unset mapped items properties. unset($item['item_id'], $item['left'], $item['right']); return $item; })->toArray(); - + DB::transaction(function () use ($reorderItems, $primaryKey, $itemKeys) { $reorderItemsBindString = implode(',', array_fill(0, count($reorderItems), '?')); - foreach(['parent_id', 'depth', 'lft', 'rgt'] as $column) { + foreach (['parent_id', 'depth', 'lft', 'rgt'] as $column) { $query = ''; - $bindings = []; + $bindings = []; $query .= "UPDATE {$this->model->getTable()} SET {$column} = CASE "; foreach ($reorderItems as $item) { $query .= "WHEN {$primaryKey} = ? THEN ? "; @@ -55,7 +55,7 @@ public function updateTreeOrder($request) DB::statement($query, $bindings); } }); - + return count($reorderItems); }