Merged [47010]: Modified GenericDataAdmin::buildResultFieldValue to accept relations of the form: obj1.obj2...objN->MethodCall, similar to TableListField_Item.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@60173 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Hayden Smith 2008-08-08 04:50:54 +00:00
parent 587394879e
commit 4144a01e14

View File

@ -380,6 +380,7 @@ HTML;
*/ */
protected function buildResultFieldValue($result, $field) { protected function buildResultFieldValue($result, $field) {
if(is_array($field)) { if(is_array($field)) {
// array-syntax
$i = 0; $i = 0;
foreach($field as $each) { foreach($field as $each) {
$value .= $i == 0 ? "" : "_"; $value .= $i == 0 ? "" : "_";
@ -387,23 +388,38 @@ HTML;
$i++; $i++;
} }
} else { } else {
$fieldParts = explode("->", $field); // This supports simple FieldName syntax
$field = $fieldParts[0]; if(strpos($field,'.') === false) {
if(preg_match('/^(.+)\.(.+)$/', $field, $matches)) { $value = ($result->val($field)) ? $result->val($field) : $result->$field;
$field = $matches[2]; // This support the syntax fieldName = Relation.RelatedField
} else {
$fieldNameParts = explode('.', $field) ;
$tmpItem = $result;
for($j=0;$j<sizeof($fieldNameParts);$j++) {
$relationMethod = $fieldNameParts[$j];
$idField = $relationMethod . 'ID';
if($j == sizeof($fieldNameParts)-1) {
$value = $tmpItem->$relationMethod;
} else {
$tmpItem = $tmpItem->$relationMethod();
}
}
$result = $tmpItem;
} }
if(isset($fieldParts[1])) { // casting
$caster = $fieldParts[1]; list ($field, $caster) = explode("->", $field);
if($caster) {
$fieldNameParts = explode('.', $field);
$fieldName = $fieldNameParts[sizeof($fieldNameParts)-1];
// When the intending value is Created.Date, the obj need to be casted as Datetime explicitely. // When the intending value is Created.Date, the obj need to be casted as Datetime explicitely.
if ($field == "Created" || $field == "LastEdited") { if ($field == "Created" || $field == "LastEdited") {
$created = Object::create('Datetime', $result->Created, "Created"); $created = Object::create('Datetime', $result->Created, "Created");
// $created->setVal();
$value = $created->val($caster); $value = $created->val($caster);
} else // Dealing with other field like "Total->Nice", etc. } else {
$value = $result->obj($field)->val($caster); // Dealing with other field like "Total->Nice", etc.
} else { // Simple field, no casting $value = $result->obj($fieldName)->val($caster);
$value = $result->val($field); }
} }
} }