From 799f0a0fe21886a1a1b5b4b83aa77f93cf91b104 Mon Sep 17 00:00:00 2001 From: Dylan Sweetensen Date: Tue, 30 Aug 2016 11:11:34 +1200 Subject: [PATCH] Finds base table to update LastEdited --- code/GridFieldOrderableRows.php | 38 +++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/code/GridFieldOrderableRows.php b/code/GridFieldOrderableRows.php index 9932de45..cd5252da 100755 --- a/code/GridFieldOrderableRows.php +++ b/code/GridFieldOrderableRows.php @@ -468,7 +468,16 @@ protected function reorderItems($list, array $values, array $sortedIDs) { // match to order the objects. if (!$isVersioned) { $sortTable = $this->getSortTable($list); - $additionalSQL = (!$list instanceof ManyManyList) ? ', "LastEdited" = NOW()' : ''; + $additionalSQL = ''; + $baseTable = $sortTable; + if(class_exists($sortTable)) { + $baseTable = singleton($sortTable)->baseTable(); + } + $isBaseTable = ($baseTable == $sortTable); + if(!$list instanceof ManyManyList && $isBaseTable){ + $additionalSQL = ', "LastEdited" = NOW()'; + } + foreach($sortedIDs as $sortValue => $id) { if($map[$id] != $sortValue) { DB::query(sprintf( @@ -479,6 +488,14 @@ protected function reorderItems($list, array $values, array $sortedIDs) { $additionalSQL, $this->getSortTableClauseForIds($list, $id) )); + + if(!$isBaseTable) { + DB::query(sprintf( + 'UPDATE "%s" SET "LastEdited" = NOW() WHERE %s', + $baseTable, + $this->getSortTableClauseForIds($list, $id) + )); + } } } } else { @@ -503,7 +520,16 @@ protected function populateSortValues(DataList $list) { $field = $this->getSortField(); $table = $this->getSortTable($list); $clause = sprintf('"%s"."%s" = 0', $table, $this->getSortField()); - $additionalSQL = (!$list instanceof ManyManyList) ? ', "LastEdited" = NOW()' : ''; + + $additionalSQL = ''; + $baseTable = $table; + if(class_exists($table)) { + $baseTable = singleton($table)->baseTable(); + } + $isBaseTable = ($baseTable == $table); + if(!$list instanceof ManyManyList && $isBaseTable){ + $additionalSQL = ', "LastEdited" = NOW()'; + } foreach($list->where($clause)->column('ID') as $id) { $max = DB::query(sprintf('SELECT MAX("%s") + 1 FROM "%s"', $field, $table)); @@ -517,6 +543,14 @@ protected function populateSortValues(DataList $list) { $additionalSQL, $this->getSortTableClauseForIds($list, $id) )); + + if(!$isBaseTable) { + DB::query(sprintf( + 'UPDATE "%s" SET "LastEdited" = NOW() WHERE %s', + $baseTable, + $this->getSortTableClauseForIds($list, $id) + )); + } } }