Fixed GridFieldOrderableRows issue when data class is Versioned and relation is has_many

This commit is contained in:
priyashantha 2018-01-30 23:21:10 +05:30
parent 6bf1a05975
commit e181bdaee7

View File

@ -18,8 +18,10 @@ use SilverStripe\ORM\DB;
use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObjectInterface; use SilverStripe\ORM\DataObjectInterface;
use SilverStripe\ORM\ManyManyList; use SilverStripe\ORM\ManyManyList;
use SilverStripe\ORM\Map;
use SilverStripe\ORM\SS_List; use SilverStripe\ORM\SS_List;
use SilverStripe\ORM\SS_Map; use SilverStripe\ORM\SS_Map;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\ViewableData; use SilverStripe\View\ViewableData;
use Exception; use Exception;
@ -171,12 +173,6 @@ class GridFieldOrderableRows extends RequestHandler implements
return $this; return $this;
} }
/**
* Gets the table which contains the sort field.
*
* @param DataList $list
* @return string
*/
public function getSortTable(SS_List $list) public function getSortTable(SS_List $list)
{ {
$field = $this->getSortField(); $field = $this->getSortField();
@ -189,12 +185,23 @@ class GridFieldOrderableRows extends RequestHandler implements
return $table; return $table;
} }
} }
return DataObject::getSchema()->tableName($this->getSortTableClass($list));
}
/**
* Gets the class which contains the sort field.
*
* @param DataList $list
* @return string
*/
public function getSortTableClass(SS_List $list)
{
$field = $this->getSortField();
$classes = ClassInfo::dataClassesFor($list->dataClass()); $classes = ClassInfo::dataClassesFor($list->dataClass());
foreach ($classes as $class) { foreach ($classes as $class) {
if (singleton($class)->hasDataBaseField($field)) { if (singleton($class)->hasDataBaseField($field)) {
return DataObject::getSchema()->tableName($class); return $class;
} }
} }
@ -499,15 +506,15 @@ class GridFieldOrderableRows extends RequestHandler implements
/** @var SS_List $map */ /** @var SS_List $map */
$map = $list->map('ID', $sortField); $map = $list->map('ID', $sortField);
//fix for versions of SS that return inconsistent types for `map` function //fix for versions of SS that return inconsistent types for `map` function
if ($map instanceof SS_Map) { if ($map instanceof Map) {
$map = $map->toArray(); $map = $map->toArray();
} }
// If not a ManyManyList and using versioning, detect it. // If not a ManyManyList and using versioning, detect it.
$isVersioned = false; $isVersioned = false;
$class = $list->dataClass(); $class = $list->dataClass();
if ($class == $this->getSortTable($list)) { if ($class == $this->getSortTableClass($list)) {
$isVersioned = $class::has_extension('SilverStripe\\ORM\\Versioning\\Versioned'); $isVersioned = $class::has_extension(Versioned::class);
} }
// Loop through each item, and update the sort values which do not // Loop through each item, and update the sort values which do not