From b54e6f12cf846cc5503aeca680467c83fe5cac89 Mon Sep 17 00:00:00 2001 From: Ed Date: Wed, 23 Jan 2013 20:11:22 -0400 Subject: [PATCH] Improved updating of sort orders to use raw queries resolves issue 4 --- code/forms/GridFieldSortableRows.php | 70 ++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 15 deletions(-) diff --git a/code/forms/GridFieldSortableRows.php b/code/forms/GridFieldSortableRows.php index 8e8d9d2..f3003a8 100644 --- a/code/forms/GridFieldSortableRows.php +++ b/code/forms/GridFieldSortableRows.php @@ -104,7 +104,6 @@ public function getManipulatedData(GridField $gridField, SS_List $dataList) { $gridField->getConfig()->removeComponentsByType('GridFieldSortableHeader'); } - return $dataList->sort($this->sortColumn); } @@ -133,12 +132,6 @@ protected function fixSortColumn($gridField, SS_List $dataList) { $max = $list->Max($this->sortColumn); if($list->where('"'.$this->sortColumn.'"=0')->Count()>0) { - //Start transaction if supported - if(DB::getConn()->supportsTransactions()) { - DB::getConn()->transactionStart(); - } - - $owner = $gridField->Form->getRecord(); $sortColumn = $this->sortColumn; $i = 1; @@ -151,18 +144,45 @@ protected function fixSortColumn($gridField, SS_List $dataList) { user_error('Sort column '.$this->sortColumn.' must be an Int, column is of type '.$fieldType, E_USER_ERROR); exit; } + }else { + //Find table containing the sort column + $table=false; + $class=$gridField->getModelClass(); + + if(!empty($class::$db) && array_key_exists($sortColumn, $class::$db)) { + $table=$class; + }else { + $classes=ClassInfo::ancestry($class, true); + foreach($classes as $class) { + if(!empty($class::$db) && array_key_exists($sortColumn, $class::$db)) { + $table=$class; + break; + } + } + } + + if($table===false) { + user_error('Sort column '.$this->sortColumn.' could not be found in '.$gridField->getModelClass().'\'s ancestry', E_USER_ERROR); + exit; + } + } + + + //Start transaction if supported + if(DB::getConn()->supportsTransactions()) { + DB::getConn()->transactionStart(); } - //@TODO Need to optimize this to eliminate some of the resource load could use raw queries to be more efficient foreach($list as $obj) { if($many_many) { DB::query('UPDATE "' . $table - . '" SET "' . $sortColumn.'" = ' . ($max + $i) + . '" SET "' . $sortColumn .'" = ' . ($max + $i) . ' WHERE "' . $componentField . '" = ' . $obj->ID . ' AND "' . $parentField . '" = ' . $owner->ID); }else { - $obj->$sortColumn = ($max + $i); - $obj->write(); + DB::query('UPDATE "' . $table + . '" SET "' . $sortColumn . '" = ' . $sortColumn = ($max + $i) + . ' WHERE "ID" = '. $obj->ID); } $i++; @@ -241,6 +261,27 @@ protected function saveGridRowSort(GridField $gridField, $data) { if ($many_many) { list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many($gridField->getName()); + }else { + //Find table containing the sort column + $table=false; + $class=$gridField->getModelClass(); + + if(!empty($class::$db) && array_key_exists($sortColumn, $class::$db)) { + $table=$class; + }else { + $classes=ClassInfo::ancestry($class, true); + foreach($classes as $class) { + if(!empty($class::$db) && array_key_exists($sortColumn, $class::$db)) { + $table=$class; + break; + } + } + } + + if($table===false) { + user_error('Sort column '.$this->sortColumn.' could not be found in '.$gridField->getModelClass().'\'s ancestry', E_USER_ERROR); + exit; + } } @@ -250,7 +291,6 @@ protected function saveGridRowSort(GridField $gridField, $data) { } - //@TODO Need to optimize this to eliminate some of the resource load could use raw queries to be more efficient $ids = explode(',', $data['ItemIDs']); for($sort = 0;$sort