Skip to content

Commit

Permalink
Finds base table to update LastEdited
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan Sweetensen committed Aug 29, 2016
1 parent 50ffec5 commit 799f0a0
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions code/GridFieldOrderableRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 {
Expand All @@ -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));
Expand All @@ -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)
));
}
}
}

Expand Down

0 comments on commit 799f0a0

Please sign in to comment.