FIX Performance issues when a blog has a large number of tags.

This commit is contained in:
micmania1 2016-03-04 00:34:10 +00:00
parent 84efcf126b
commit 0795ef721b

View File

@ -2,6 +2,26 @@
class GridFieldMergeAction implements GridField_ColumnProvider, GridField_ActionProvider
{
/**
* Cache results of records queries. This is useful for blogs
* with a lot of tags or categories.
*
* @var array
*
* @example
* array(1) {
* ["2d73dd8f18b8da3c8e5e75b6bee66d8d"]=> array(3) {
* [2]=> string(5) "tag2"
* [1]=> string(6) "tag1"
* [3]=> string(4) "tag3"
* }
* }
* The array key in the first level is made up of an md5 hash of the sql query,
* then the second level is made if up ID => tag name.
*/
protected static $cached_results = array();
/**
* List of records to show in the MergeAction column.
*
@ -69,8 +89,28 @@ class GridFieldMergeAction implements GridField_ColumnProvider, GridField_Action
*/
public function getColumnContent($gridField, $record, $columnName)
{
if ($columnName === 'MergeAction' && $record->{$this->childMethod}()->Count() > 0) {
$dropdown = new DropdownField('Target', 'Target', $this->records->exclude('ID', $record->ID)->map());
if ($columnName === 'MergeAction') {
if(!$record->hasMethod($this->childMethod) || $record->{$this->childMethod}()->count() < 1) {
return '';
}
$children = $record->{$this->childMethod}();
$cacheKey = md5($children->dataQuery()->sql());
if(isset(self::$cached_results[$cacheKey])) {
$data = self::$cached_results[$cacheKey];
} else {
$data = $this->records->map()->toArray();
self::$cached_results[$cacheKey] = $data;
}
// Unset the current record
if(isset($data[$record->ID])) {
unset($data[$record->ID]);
}
if(count($data) > 0) {
$dropdown = new DropdownField('Target', 'Target', $data);
$dropdown->setAttribute('id', 'Target_'.$record->ID);
$prefix = strtolower($this->parentMethod . '-' . $this->childMethod);
@ -91,6 +131,7 @@ class GridFieldMergeAction implements GridField_ColumnProvider, GridField_Action
return $dropdown->Field() . $action->Field() . '<a title="Move posts to" class="MergeActionReveal">move posts to</a>';
}
}
return null;
}