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
This commit is contained in:
Ingo Schommer 2008-10-02 13:58:08 +00:00
parent 0eff9f7e62
commit 892becb520

View File

@ -45,14 +45,8 @@
* if(!$member) $member = Member::currentUser(); * if(!$member) $member = Member::currentUser();
* return $member->inGroup('Editors'); * 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');
* }
* } * }
* </code> * </code>
* *
@ -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. * 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". * 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() * 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); $fieldName = array_pop($relations);
$relObj = $this; $relObj = $this;
foreach($relations as $i=>$relation) { foreach($relations as $i=>$relation) {
$relObj = $relObj->$relation(); // no support for has_many or many_many relationships,
// If the intermediate relationship objects have been created, then write them // as the updater wouldn't know which object to write to (or create)
if($i<sizeof($relation)-1 && !$relObj->ID) $relObj->write(); if($relObj->has_one($relation)) {
$relObj = $relObj->$relation();
// If the intermediate relationship objects have been created, then write them
if($i<sizeof($relation)-1 && !$relObj->ID) $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) { if($relObj) {
$relObj->$fieldName = $v; $relObj->$fieldName = $v;