Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Problem with Page->duplicateWithChildren()

Go to End

4 Posts   1581 Views


Community Member, 7 Posts

13 March 2010 at 10:10am

I have a class 'CourseTemplatePage' which extends the Page class. I want to create a clone of this page when the CloneAsTemplate action is called.

function CloneAsTemplate($arguments)
	$data = $arguments->allParams();
	if($ct = DataObject::get_by_id('CourseTemplatePage', $data['ID']))
		$newct = $ct->duplicateWithChildren();
		$newct->Title .= ' (Clone)';
		$newct->publish('Stage', 'Live');

The this code gets to the publish command, an Warning occurs, stating that the record didn't exist in Stage.
Upon inspecting the database, everything seems to be correct, with proper entries in the CourseTemplatePage, CourseTemplatePage_Live, CourseTemplatePage_version, etc...

However, when I took a look at the record in SiteTree, I saw the record had a ClassName of 'SiteTree' instead of 'CourseTemplatePage'. Looking at the code for the Versioned class, it appears that the class name must properly match in order for publish to work.

Is there something wrong in my code that is causing duplicateWithChildren() to incorrectly classify the CourseTemplatePage? Or is there something else that I am glossing over? Any help would be appreciated.



Community Member, 131 Posts

14 March 2010 at 3:16pm

Edited: 14/03/2010 3:19pm

just curious:
have you tried to duplicate with children manually (by rightclicking in the sitetree-pane)?
if you do so, what happens (especially in SiteTree database)?


Community Member, 7 Posts

16 March 2010 at 2:13am

Thanks for your response.

When I clone a page and its children though the CMS SiteTree pane, the SiteTree table has ClassName appropriately set--in this case CourseTemplatePage.

Am I using the duplicateWithChildren method correctly? Is my publishing code set up properly? Is there something in my class that could alter the way duplicateWithChildren works?

Thanks again for your help,


Community Member, 7 Posts

16 March 2010 at 4:42am

Here's what I have:

Inside my CourseTemplatePage class I copied the duplicateWithChildren method from SiteTree, but with one subtle difference: when the initial object's duplicate call is made, my method specifies that the duplicate should not have the write() method called:

public function duplicateWithChildren()
	// Changed this line to specify that the write() method should not be called
	// Without this, writeToStage('Stage') doesn't work properly.
	// $clone = $this->duplicate();
	$clone = $this->duplicate(False);
	$children = $this->AllChildren();

	if($children) {
		foreach($children as $child) {
			$childClone = method_exists($child, 'duplicateWithChildren')
				? $child->duplicateWithChildren()
				: $child->duplicate();
			$childClone->ParentID = $clone->ID;

	return $clone;

If someone reads this and understands why this fixes the problem I was having, please let me know as this is something I would like to get my head around.