Added support for overriding the relationship name lookup (issue #86)

This commit is contained in:
Ed Chipman 2016-02-09 13:26:34 -04:00
parent 1eb946a8a5
commit d37fdd2ea9
2 changed files with 28 additions and 6 deletions

View File

@ -61,6 +61,14 @@ $myGridConfig->addComponent($sortable=new GridFieldSortableRows('SortOrder'));
$sortable->setUpdateVersionedStage('Live');
```
#### Overriding the default relationship name
By default the relationship name comes from the name of the GridField, however you can override this lookup by calling setCustomRelationName() and passing in the name of the relationship. This allows for you to have multiple GridFields on the same form interacting with the same many_many list maybe filtered slightly differently.
```php
$myGridConfig->addComponent($sortable=new GridFieldSortableRows('SortOrder'));
$sortable->setCustomRelationName('MyRelationship');
```
## Migrating from SilverStripe 2.4 and Data Object Manager's SortableDataObject
SortableGridField is not the same as SortableDataObject, since it is only a component of GridField it does not have the ability to catch the object when it is saved for the first time. So SortableGridField uses 1 as the first sort index because 0 is the default for an integer field/column in the database. For migrations from 2.4 with SortableDataObject you need to setup your DataObject based on the instructions above however you must name your sort column "SortOrder" to maintain your sort indexes defined by SortableDataObject. Then you need to run the following query on the table containing your sort field, for many_many relationships this will be something like {RelationshipClass}_{RelationshipName}. This query will maintain your sort order from SortableDataObject but increment the index by 1 giving it a starting number of 1.

View File

@ -9,16 +9,19 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
protected $disable_selection=true;
protected $append_to_top=false;
protected $update_versioned_stage=null;
protected $custom_relation_name=null;
/**
* @param string $sortColumn Column that should be used to update the sort information
* @param bool $disableSelection Disable selection on the GridField when dragging
* @param string $updateVersionStage Name of the versioned stage to update this disabled by default unless this is set
* @param string $customRelatinoName Name of the relationship to use, if left null the name is determined from the GridField's name
*/
public function __construct($sortColumn, $disableSelection = true, $updateVersionStage = null) {
public function __construct($sortColumn, $disableSelection = true, $updateVersionStage = null, $customRelationName = null) {
$this->sortColumn = $sortColumn;
$this->disable_selection = $disableSelection;
$this->update_versioned_stage = $updateVersionStage;
$this->custom_relation_name = $customRelationName;
}
/**
@ -136,7 +139,7 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
$this->append_to_top=$value;
return $this;
}
/**
* @param bool $value Boolean true to disable selection of table contents false to enable selection
* @return GridFieldSortableRows Returns the current instance
@ -145,6 +148,7 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
$this->disable_selection = $value;
return $this;
}
/**
* Sets the suffix of the versioned stage that should be updated along side the default stage
* @param string $value Versioned Stage to update this is disabled by default unless this is set
@ -155,6 +159,16 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
return $this;
}
/**
* Sets the name of the relationship to use, by default the name is determined from the GridField's name
* @param string $value Name of the relationship to use, by default the name is determined from the GridField's name
* @return GridFieldSortableRows Returns the current instance
*/
public function setCustomRelationName($value) {
$this->custom_relation_name=$value;
return $this;
}
/**
* Detects and corrects items with a sort column value of 0, by appending them to the bottom of the list
* @param GridField $gridField Grid Field Reference
@ -195,8 +209,8 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
$i = 1;
if ($many_many) {
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many($gridField->getName());
$extraFields=$owner->many_many_extraFields($gridField->getName());
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many((!empty($this->custom_relation_name) ? $this->custom_relation_name:$gridField->getName()));
$extraFields=$owner->many_many_extraFields((!empty($this->custom_relation_name) ? $this->custom_relation_name:$gridField->getName()));
if(!$extraFields || !array_key_exists($this->sortColumn, $extraFields) || !($extraFields[$this->sortColumn]=='Int' || is_subclass_of('Int', $extraFields[$this->sortColumn]))) {
user_error('Sort column '.$this->sortColumn.' must be an Int, column is of type '.$extraFields[$this->sortColumn], E_USER_ERROR);
@ -388,7 +402,7 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
if ($many_many) {
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many($gridField->getName());
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many((!empty($this->custom_relation_name) ? $this->custom_relation_name:$gridField->getName()));
}else {
//Find table containing the sort column
$table=false;
@ -516,7 +530,7 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
if ($many_many) {
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many($gridField->getName());
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many((!empty($this->custom_relation_name) ? $this->custom_relation_name:$gridField->getName()));
}