mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
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:
parent
b9cb101182
commit
9bf204d8f1
@ -439,33 +439,34 @@ class SiteTree extends DataObject {
|
||||
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.
|
||||
*
|
||||
* @return SiteTree Parent of this page.
|
||||
*/
|
||||
public function getParent() {
|
||||
if($this->getField("ParentID"))
|
||||
return DataObject::get_one("SiteTree",
|
||||
"`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);
|
||||
if ($this->getField("ParentID")) {
|
||||
return DataObject::get_one("SiteTree", "`SiteTree`.ID = " . $this->getField("ParentID"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return a string of the form "parent - page" or
|
||||
* "grandparent - parent - page".
|
||||
|
@ -42,12 +42,27 @@ class SiteTreeTest extends SapphireTest {
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that saving changes creates a new version with the correct data in it.
|
||||
*/
|
||||
function testParentModelReturnType() {
|
||||
$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 { }
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user