From 892becb520d9be20b8cfea1524d3288baad9466f Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 2 Oct 2008 13:58:08 +0000 Subject: [PATCH] ENHANCMENT Checking for valid has_one relationship on dot-notation-usage in DataObject->update MINOR Documentation in DataObject git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63531 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/DataObject.php | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/core/model/DataObject.php b/core/model/DataObject.php index 3b3e9e16f..fbba3bfeb 100644 --- a/core/model/DataObject.php +++ b/core/model/DataObject.php @@ -45,14 +45,8 @@ * if(!$member) $member = Member::currentUser(); * return $member->inGroup('Editors'); * } - * public function canDelete($member = false) { - * if(!$member) $member = Member::currentUser(); - * return $member->inGroup('Editors'); - * } - * public function canCreate($member = false) { - * if(!$member) $member = Member::currentUser(); - * return $member->inGroup('Editors'); - * } + * + * // ... * } * * @@ -394,7 +388,7 @@ class DataObject extends ViewableData implements DataObjectInterface { /** * Update a number of fields on this object, given a map of the desired changes. * - * The field names can be simple names, or you can use a dot syntax to access relations. + * The field names can be simple names, or you can use a dot syntax to access $has_one relations. * For example, array("Author.FirstName" => "Jim") will set $this->Author()->FirstName to "Jim". * * update() doesn't write the main object, but if you use the dot syntax, it will write() @@ -410,9 +404,23 @@ class DataObject extends ViewableData implements DataObjectInterface { $fieldName = array_pop($relations); $relObj = $this; foreach($relations as $i=>$relation) { - $relObj = $relObj->$relation(); - // If the intermediate relationship objects have been created, then write them - if($iID) $relObj->write(); + // no support for has_many or many_many relationships, + // as the updater wouldn't know which object to write to (or create) + if($relObj->has_one($relation)) { + $relObj = $relObj->$relation(); + + // If the intermediate relationship objects have been created, then write them + if($iID) $relObj->write(); + } else { + user_error( + "DataObject::update(): Can't traverse relationship '$relation'," . + "it has to be a has_one relationship returning a single DataObject", + E_USER_NOTICE + ); + // unset relation object so we don't write properties to the wrong object + unset($relObj); + break; + } } if($relObj) { $relObj->$fieldName = $v;