update to resolve #2653 - ensure getParent casts to correct subclass (always worked) and ensure setParent resolves valid ID of passed in object before updating its ParentID.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@57950 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Mark Rickerby 2008-07-14 11:09:44 +00:00
parent b9cb101182
commit 9bf204d8f1
2 changed files with 41 additions and 25 deletions

View File

@ -439,33 +439,34 @@ class SiteTree extends DataObject {
return implode(self::$breadcrumbs_delimiter, array_reverse($parts)); return implode(self::$breadcrumbs_delimiter, array_reverse($parts));
} }
/**
* Make this page a child of another page.
*
* If the parent page does not exist, resolve it to a valid ID
* before updating this page's reference.
*
* @param SiteTree|int $item Either the parent object, or the parent ID
*/
public function setParent($item) {
if(is_object($item)) {
if (!$item->exists()) $item->write();
$this->setField("ParentID", $item->ID);
} else {
$this->setField("ParentID", $item);
}
}
/** /**
* Get the parent of this page. * Get the parent of this page.
* *
* @return SiteTree Parent of this page. * @return SiteTree Parent of this page.
*/ */
public function getParent() { public function getParent() {
if($this->getField("ParentID")) if ($this->getField("ParentID")) {
return DataObject::get_one("SiteTree", return DataObject::get_one("SiteTree", "`SiteTree`.ID = " . $this->getField("ParentID"));
"`SiteTree`.ID = " . $this->getField("ParentID"));
}
/**
* Make this page a child of another page.
*
* @param SiteTree|int $item Either the parent object, or the parent ID
*/
public function setParent($item) {
if(is_object($item)) {
$this->setField("ParentID", $item->ID);
} else {
$this->setField("ParentID", $item);
} }
} }
/** /**
* Return a string of the form "parent - page" or * Return a string of the form "parent - page" or
* "grandparent - parent - page". * "grandparent - parent - page".

View File

@ -42,12 +42,27 @@ class SiteTreeTest extends SapphireTest {
$this->assertEquals($obj->ID, $createdID); $this->assertEquals($obj->ID, $createdID);
} }
function testParentNodeCachedInMemory() {
$parent = new SiteTree();
$parent->Title = 'Section Title';
$child = new SiteTree();
$child->Title = 'Page Title';
$child->setParent($parent);
$this->assertType("SiteTree", $child->Parent);
$this->assertEquals("Section Title", $child->Parent->Title);
}
/** function testParentModelReturnType() {
* Test that saving changes creates a new version with the correct data in it. $parent = new PageNode();
*/ $child = new PageNode();
$child->setParent($parent);
$this->assertType('PageNode', $child->Parent);
}
/** }
* Test that publishing an unsaved page will save the changes before publishing
*/ class PageNode extends SiteTree { }
}
?>