From d7774d66736d4f6f522ceba2dc3c735a0808be2c Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 28 Mar 2018 10:07:55 +1300 Subject: [PATCH] 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) )); }