ENHANCEMENT: ComplexTableFilters used to edit relations have their filter automatically set, as well as the foreign key on new records.

API CHANGE: TableListField::sourceFilter() can be overloaded to change the querying logic.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@62883 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2008-09-23 01:24:03 +00:00
parent fcc687fff2
commit ed311db3bf
2 changed files with 36 additions and 2 deletions

View File

@ -180,6 +180,24 @@ class ComplexTableField extends TableListField {
} }
/**
* Return the record filter for this table.
* It will automatically add a relation filter if relationAutoSetting is true, and it can determine an appropriate
* filter.
*/
function sourceFilter() {
$sourceFilter = parent::sourceFilter();
if($this->relationAutoSetting
&& $this->getParentClass()
&& ($filterKey = $this->getParentIdName($this->getParentClass(), $this->sourceClass()))
&& ($filterValue = $this->sourceID()) ) {
$newFilter = "`$filterKey` = '" . Convert::raw2sql($filterValue) . "'";
$sourceFilter = $sourceFilter ? "($sourceFilter) AND ($newFilter)" : $newFilter;
}
return $sourceFilter;
}
function isComposite() { function isComposite() {
return false; return false;
} }
@ -486,6 +504,15 @@ JS;
return $form; return $form;
} }
/**
* By default, a ComplexTableField will assume that the field name is the name of a has-many relation on the object being
* edited. It will identify the foreign key in the object being listed, and filter on that column, as well as auto-setting
* that column for newly created records.
*
* Calling $this->setRelationAutoSetting(false) will disable this functionality.
*
* @param boolean $value Should the relation auto-setting functionality be enabled?
*/
function setRelationAutoSetting($value) { function setRelationAutoSetting($value) {
$this->relationAutoSetting = $value; $this->relationAutoSetting = $value;
} }

View File

@ -247,6 +247,13 @@ class TableListField extends FormField {
Requirements::css('sapphire/css/TableListField.css'); Requirements::css('sapphire/css/TableListField.css');
} }
/**
* Get the filter
*/
function sourceFilter() {
return $this->sourceFilter;
}
function index() { function index() {
return $this->FieldHolder(); return $this->FieldHolder();
} }
@ -421,7 +428,7 @@ JS
$query->select[] = "{$baseClass}.ClassName AS ClassName"; $query->select[] = "{$baseClass}.ClassName AS ClassName";
$query->select[] = "{$baseClass}.ClassName AS RecordClassName"; $query->select[] = "{$baseClass}.ClassName AS RecordClassName";
} else { } else {
$query = singleton($this->sourceClass)->extendedSQL($this->sourceFilter, $this->sourceSort, null, $this->sourceJoin); $query = singleton($this->sourceClass)->extendedSQL($this->sourceFilter(), $this->sourceSort, null, $this->sourceJoin);
// Add more selected fields if they are from joined table. // Add more selected fields if they are from joined table.
@ -456,7 +463,7 @@ JS
$query->select[] = "{$baseClass}.ClassName AS ClassName"; $query->select[] = "{$baseClass}.ClassName AS ClassName";
$query->select[] = "{$baseClass}.ClassName AS RecordClassName"; $query->select[] = "{$baseClass}.ClassName AS RecordClassName";
} else { } else {
$query = singleton($this->sourceClass)->extendedSQL($this->sourceFilter, $this->sourceSort, null, $this->sourceJoin); $query = singleton($this->sourceClass)->extendedSQL($this->sourceFilter(), $this->sourceSort, null, $this->sourceJoin);
// Add more selected fields if they are from joined table. // Add more selected fields if they are from joined table.
foreach($this->FieldList() as $k=>$title){ foreach($this->FieldList() as $k=>$title){