BUGFIX #3248: Fixed TreeDropdownField when using non-ID key field

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@69696 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2009-01-05 04:45:46 +00:00
parent 5f9120e99e
commit 4f32e1873b

View File

@ -40,7 +40,7 @@ class TreeDropdownField extends FormField {
Requirements::javascript(SAPPHIRE_DIR . "/javascript/TreeSelectorField.js"); Requirements::javascript(SAPPHIRE_DIR . "/javascript/TreeSelectorField.js");
if($this->value) { if($this->value) {
$record = DataObject::get_by_id($this->sourceObject, $this->value); $record = $this->getByKey($this->value);
$title = ($record) ? $record->Title : _t('DropdownField.CHOOSE', "(Choose)", PR_MEDIUM, 'Start-value of a dropdown'); $title = ($record) ? $record->Title : _t('DropdownField.CHOOSE', "(Choose)", PR_MEDIUM, 'Start-value of a dropdown');
} else { } else {
$title = _t('DropdownField.CHOOSE', "(Choose)", PR_MEDIUM, 'Start-value of a dropdown'); $title = _t('DropdownField.CHOOSE', "(Choose)", PR_MEDIUM, 'Start-value of a dropdown');
@ -86,8 +86,7 @@ HTML;
* Return a subtree via Ajax * Return a subtree via Ajax
*/ */
public function getsubtree() { public function getsubtree() {
if($this->keyField == "ID") $obj = DataObject::get_by_id($this->sourceObject, $_REQUEST['SubtreeRootID']); $obj = $this->getByKey($_REQUEST['SubtreeRootID']);
else $obj = DataObject::get_one($this->sourceObject, "$this->keyField = '$_REQUEST[SubtreeRootID]'");
if(!$obj) user_error("Can't find database record $this->sourceObject with $this->keyField = $_REQUEST[SubtreeRootID]", E_USER_ERROR); if(!$obj) user_error("Can't find database record $this->sourceObject with $this->keyField = $_REQUEST[SubtreeRootID]", E_USER_ERROR);
if($this->filterFunc) $obj->setMarkingFilterFunction($this->filterFunc); if($this->filterFunc) $obj->setMarkingFilterFunction($this->filterFunc);
@ -99,14 +98,11 @@ HTML;
} }
public function getByKey($key) { public function getByKey($key) {
if(!is_numeric($key)) {
return false;
}
if($this->keyField == 'ID') { if($this->keyField == 'ID') {
return DataObject::get_by_id($this->sourceObject, $key); return DataObject::get_by_id($this->sourceObject, $key);
} else { } else {
return DataObject::get_one($this->sourceObject, "$this->keyField = '$key'"); $SQL_key = Convert::raw2sql($key);
return DataObject::get_one($this->sourceObject, "$this->keyField = '$SQL_key'");
} }
} }
@ -114,8 +110,7 @@ HTML;
* Return the stack of values to be traversed to find the given key in the database * Return the stack of values to be traversed to find the given key in the database
*/ */
public function getstack() { public function getstack() {
if($this->keyField == "ID") $page = DataObject::get_by_id($this->sourceObject, $_REQUEST['SubtreeRootID']); $page = $this->getByKey($_REQUEST['SubtreeRootID']);
else $page = $this->getByKey($_REQUEST['SubtreeRootID']);
while($page->ParentID) { while($page->ParentID) {
echo $ids[] = $page->ID; echo $ids[] = $page->ID;