API CHANGE: Added support for dot syntax to FieldSet::fieldByName()

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63922 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2008-10-09 00:32:10 +00:00
parent 3d4d4d0c42
commit fce3767389

View File

@ -240,15 +240,30 @@ class FieldSet extends DataObjectSet {
/** /**
* Returns a named field. * Returns a named field.
* You can use dot syntax to get fields from child composite fields
* *
* @todo Implement similiarly to dataFieldByName() to support nested sets - or merge with dataFields() * @todo Implement similiarly to dataFieldByName() to support nested sets - or merge with dataFields()
*/ */
public function fieldByName($name) { public function fieldByName($name) {
if(strpos($name,'.') !== false) list($name, $remainder) = explode('.',$name,2);
else $remainder = null;
foreach($this->items as $child) { foreach($this->items as $child) {
if($name == $child->Name() || $name == $child->id) return $child; if($name == $child->Name() || $name == $child->id) {
if($remainder) {
if($child->isComposite()) {
return $child->fieldByName($remainder);
} else {
user_error("Trying to get field '$remainder' from non-composite field $child->class.$name", E_USER_WARNING);
return null;
}
} else {
return $child;
}
}
} }
} }
/** /**
* Returns a named field in a sequential set. * Returns a named field in a sequential set.
* Use this if you're using nested FormFields. * Use this if you're using nested FormFields.