From d7774d66736d4f6f522ceba2dc3c735a0808be2c Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 28 Mar 2018 10:07:55 +1300 Subject: [PATCH 1/2] Use DBQuery to update the stage & live sort field Use DBQuery to update the stage & live sort field. Also check if the list is a many_many list when reordering items --- src/GridFieldOrderableRows.php | 55 ++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/src/GridFieldOrderableRows.php b/src/GridFieldOrderableRows.php index 310dcee..2b5c7a9 100755 --- a/src/GridFieldOrderableRows.php +++ b/src/GridFieldOrderableRows.php @@ -2,6 +2,7 @@ namespace Symbiote\GridFieldExtensions; +use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Control\Controller; use SilverStripe\Control\RequestHandler; use SilverStripe\Core\ClassInfo; @@ -17,11 +18,11 @@ use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObjectInterface; +use SilverStripe\ORM\DataObjectSchema; use SilverStripe\ORM\DB; use SilverStripe\ORM\ManyManyList; use SilverStripe\ORM\Map; use SilverStripe\ORM\SS_List; -use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\Versioned\Versioned; use SilverStripe\View\ViewableData; @@ -211,19 +212,24 @@ class GridFieldOrderableRows extends RequestHandler implements public function getSortTable(SS_List $list) { $field = $this->getSortField(); + if ($list instanceof ManyManyList) { $extra = $list->getExtraFields(); $table = $list->getJoinTable(); + if ($extra && array_key_exists($field, $extra)) { return $table; } } + $classes = ClassInfo::dataClassesFor($list->dataClass()); + foreach ($classes as $class) { if (singleton($class)->hasDataBaseField($field)) { return DataObject::getSchema()->tableName($class); } } + throw new \Exception("Couldn't find the sort field '$field'"); } @@ -531,24 +537,21 @@ class GridFieldOrderableRows extends RequestHandler implements // If not a ManyManyList and using versioning, detect it. $this->validateSortField($list); - $isVersioned = false; $class = $list->dataClass(); - - if (DataObject::getSchema()->tableName($class) == $this->getSortTable($list)) { - $isVersioned = $class::has_extension(Versioned::class); - } + $isVersioned = $class::has_extension(Versioned::class); // Loop through each item, and update the sort values which do not // match to order the objects. - if (!$isVersioned) { + if (!$isVersioned || $list instanceof ManyManyList) { $sortTable = $this->getSortTable($list); - $now = DBDatetime::now()->Rfc2822(); $additionalSQL = ''; - $baseTable = DataObject::getSchema()->baseDataTable($list->dataClass()); - + $baseTable = $sortTable; + if (class_exists($sortTable)) { + $baseTable = singleton($sortTable)->baseTable(); + } $isBaseTable = ($baseTable == $sortTable); if (!$list instanceof ManyManyList && $isBaseTable) { - $additionalSQL = ", \"LastEdited\" = '$now'"; + $additionalSQL = ', "LastEdited" = NOW()'; } foreach ($sortedIDs as $sortValue => $id) { @@ -562,11 +565,10 @@ class GridFieldOrderableRows extends RequestHandler implements $this->getSortTableClauseForIds($list, $id) )); - if (!$isBaseTable && !$list instanceof ManyManyList) { + if (!$isBaseTable) { DB::query(sprintf( - 'UPDATE "%s" SET "LastEdited" = \'%s\' WHERE %s', + 'UPDATE "%s" SET "LastEdited" = NOW() WHERE %s', $baseTable, - $now, $this->getSortTableClauseForIds($list, $id) )); } @@ -580,8 +582,14 @@ class GridFieldOrderableRows extends RequestHandler implements foreach ($sortedIDs as $sortValue => $id) { if ($map[$id] != $sortValue) { $record = $class::get()->byID($id); - $record->$sortField = $sortValue; - $record->write(); + if ($record instanceof SiteTree) { + $tableClass = 'SiteTree'; + } else { + $schema = new DataObjectSchema(); + $tableClass = $schema->tableName($class); + } + DB::query("UPDATE " . $tableClass . " SET " . $sortField . "=" . $sortValue . " WHERE ID=" . $record->ID); + DB::query("UPDATE " . $tableClass . "_Live SET " . $sortField . "=" . $sortValue. " WHERE ID=" . $record->ID); } } } @@ -595,13 +603,15 @@ class GridFieldOrderableRows extends RequestHandler implements $field = $this->getSortField(); $table = $this->getSortTable($list); $clause = sprintf('"%s"."%s" = 0', $table, $this->getSortField()); - $now = DBDatetime::now()->Rfc2822(); - $additionalSQL = ''; - $baseTable = DataObject::getSchema()->baseDataTable($list->dataClass()); + $additionalSQL = ''; + $baseTable = $table; + if (class_exists($table)) { + $baseTable = singleton($table)->baseTable(); + } $isBaseTable = ($baseTable == $table); if (!$list instanceof ManyManyList && $isBaseTable) { - $additionalSQL = ", \"LastEdited\" = '$now'"; + $additionalSQL = ', "LastEdited" = NOW()'; } foreach ($list->where($clause)->column('ID') as $id) { @@ -617,11 +627,10 @@ class GridFieldOrderableRows extends RequestHandler implements $this->getSortTableClauseForIds($list, $id) )); - if (!$isBaseTable && !$list instanceof ManyManyList) { + if (!$isBaseTable) { DB::query(sprintf( - 'UPDATE "%s" SET "LastEdited" = \'%s\' WHERE %s', + 'UPDATE "%s" SET "LastEdited" = NOW() WHERE %s', $baseTable, - $now, $this->getSortTableClauseForIds($list, $id) )); } From fcca629d7db3d04cf94fc6c798cdcbab8d4e82b7 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 24 Apr 2018 13:18:53 +1200 Subject: [PATCH 2/2] Revert back to using DBDatetime::now()->Rfc2822() --- src/GridFieldOrderableRows.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/GridFieldOrderableRows.php b/src/GridFieldOrderableRows.php index 2b5c7a9..60d8229 100755 --- a/src/GridFieldOrderableRows.php +++ b/src/GridFieldOrderableRows.php @@ -20,6 +20,7 @@ use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObjectInterface; use SilverStripe\ORM\DataObjectSchema; use SilverStripe\ORM\DB; +use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\ORM\ManyManyList; use SilverStripe\ORM\Map; use SilverStripe\ORM\SS_List; @@ -544,6 +545,7 @@ class GridFieldOrderableRows extends RequestHandler implements // match to order the objects. if (!$isVersioned || $list instanceof ManyManyList) { $sortTable = $this->getSortTable($list); + $now = DBDatetime::now()->Rfc2822(); $additionalSQL = ''; $baseTable = $sortTable; if (class_exists($sortTable)) { @@ -551,7 +553,7 @@ class GridFieldOrderableRows extends RequestHandler implements } $isBaseTable = ($baseTable == $sortTable); if (!$list instanceof ManyManyList && $isBaseTable) { - $additionalSQL = ', "LastEdited" = NOW()'; + $additionalSQL = ", \"LastEdited\" = '$now'"; } foreach ($sortedIDs as $sortValue => $id) {