Permalink
Browse files

Improved updating of sort orders to use raw queries resolves issue 4

  • Loading branch information...
UndefinedOffset committed Jan 24, 2013
1 parent d4e8ba6 commit b54e6f12cf846cc5503aeca680467c83fe5cac89
Showing with 55 additions and 15 deletions.
  1. +55 −15 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<count($ids);$sort++) {
$id = intval($ids[$sort]);
@@ -259,9 +299,9 @@ protected function saveGridRowSort(GridField $gridField, $data) {
. '" SET "' . $sortColumn.'" = ' . (($sort + 1) + $pageOffset)
. ' WHERE "' . $componentField . '" = ' . $id . ' AND "' . $parentField . '" = ' . $owner->ID);
} else {
- $obj = $items->byID($ids[$sort]);
- $obj->$sortColumn = ($sort + 1) + $pageOffset;
- $obj->write();
+ DB::query('UPDATE "' . $table
+ . '" SET "' . $sortColumn . '" = ' . (($sort + 1) + $pageOffset)
+ . ' WHERE "ID" = '. $id);
}
}

0 comments on commit b54e6f1

Please sign in to comment.