From 4144a01e1435c62ac3b84e54319ec79de864c6ea Mon Sep 17 00:00:00 2001 From: Hayden Smith Date: Fri, 8 Aug 2008 04:50:54 +0000 Subject: [PATCH] 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 --- code/GenericDataAdmin.php | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/code/GenericDataAdmin.php b/code/GenericDataAdmin.php index c1553fa9..bca15b04 100755 --- a/code/GenericDataAdmin.php +++ b/code/GenericDataAdmin.php @@ -380,6 +380,7 @@ HTML; */ protected function buildResultFieldValue($result, $field) { if(is_array($field)) { + // array-syntax $i = 0; foreach($field as $each) { $value .= $i == 0 ? "" : "_"; @@ -387,23 +388,38 @@ HTML; $i++; } } else { - $fieldParts = explode("->", $field); - $field = $fieldParts[0]; - if(preg_match('/^(.+)\.(.+)$/', $field, $matches)) { - $field = $matches[2]; + // This supports simple FieldName syntax + if(strpos($field,'.') === false) { + $value = ($result->val($field)) ? $result->val($field) : $result->$field; + // This support the syntax fieldName = Relation.RelatedField + } else { + $fieldNameParts = explode('.', $field) ; + $tmpItem = $result; + for($j=0;$j$relationMethod; + } else { + $tmpItem = $tmpItem->$relationMethod(); + } + } + $result = $tmpItem; } - if(isset($fieldParts[1])) { - $caster = $fieldParts[1]; + // casting + 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. if ($field == "Created" || $field == "LastEdited") { $created = Object::create('Datetime', $result->Created, "Created"); - // $created->setVal(); $value = $created->val($caster); - } else // Dealing with other field like "Total->Nice", etc. - $value = $result->obj($field)->val($caster); - } else { // Simple field, no casting - $value = $result->val($field); + } else { + // Dealing with other field like "Total->Nice", etc. + $value = $result->obj($fieldName)->val($caster); + } } }