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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

Automatically Creating Child Pages


Go to End


27 Posts   8787 Views

Avatar
theAlien

Community Member, 131 Posts

30 March 2009 at 9:03am

Ah, thanks Aram. I'm still figuring out when things have to go in the controller and when they have to go in the normal extends Page-place.

Avatar
Carbon Crayon

Community Member, 598 Posts

30 March 2009 at 12:15pm

Edited: 30/03/2009 12:52pm

Hi Alien

Just think of it like this. Anything that has to do with defining the data or setting the data when the page is created is done in the 'Model' as well as any functions which deal with retrieving or storing data from/to that page in the database.

The 'Controller' is where all of the logic for that page type goes, so all the functions that need to be run while a user opens and browses that page go into the controller.

Hope that makes it a little clearer, I remember I found it confusing at the start too :)

Avatar
snaip

Community Member, 181 Posts

5 November 2009 at 10:07pm

Edited: 05/11/2009 10:12pm

the problem comes again :(

class DzienPage extends SiteTree {
	
	public static $db = array(
	);
	
	public static $has_one = array(
	);

	static $allowed_children = array('ProgramDniaPage, SylwetkiHolder, SprawozdaniaHolder');

	function onAfterWrite() {
		
		if(!$this->Children()->count()){

			$program_dnia = new ProgramDniaPage();
			$program_dnia->ParentID = $this->ID;
			$program_dnia->Title = "Program dnia";
			$program_dnia->write();

			$sylwetki = new SylwetkiHolder();
			$sylwetki->ParentID = $this->ID;
			$sylwetki->Title = "Sylwetki";
			$sylwetki->write();

			$sprawozdania = new SprawozdaniaHolder();
			$sprawozdania->ParentID = $this->ID;
			$sprawozdania->Title = "Sprawozdania";
			$sprawozdania->write();

		}
		return parent::onAfterWrite(); 
	}
}

works
but when i publish NewDzienPage i get three blank pages which are copies of ProgramDniaPage, SylwetkiPage and SprawozdaniaHolder

look at screen

SS 2.3.2

Attached Files
Avatar
snaip

Community Member, 181 Posts

5 November 2009 at 10:39pm

the solution could be first publish the subpages and NewDzienPage at the end
but i can't find how to change default behaviour

i tired

	$sprawozdania->Status = "Published";

and in SprawozdzaniaHolder class

	static $defaults = array( 
		"Status" => 'Published'
	);

but nothing works

Avatar
Tuckie

Community Member, 10 Posts

20 August 2011 at 8:31am

Old thread, but has anyone gotten this working reliably?

I get draft and published duplicate children if I try save and publish, and with the "duplicate this page and children" function, I get draft duplicates.

my code:

	function onAfterWrite() {
		if(!$this->Children()->Count()){ 
			$subpage = new JobHolder(); 
			$subpage->ParentID = $this->ID; 
			$subpage->Title = "Jobs"; 
			$subpage->write();
		}
		return parent::onAfterWrite();
	}

Avatar
henkrid

Community Member, 2 Posts

25 August 2011 at 3:48am

Edited: 25/08/2011 3:57am

Same problem with same code as Tuckie.
The problem occures when you "save & publish" the parent-page while the generated child-page is still unpublished.
When you publish the child-page before the parent-page, everything works fine.

This problem is caused by the onAfterWrite-Event. During the "save & publish" process it gets triggered four times.
At fourth time (triggerd by the "real" publish process) $this->Children()->Count() is 0 because the parent-page has no published child yet.

Here's a reference from silverstripe googlegroups:
"1. LeftAndMain calls DataObjects writeWithoutVersion which calls write
2. LeftAndMain calls write on the $record - the actual save I believe
3. LeftAndMain calls doPublish on SiteTree which calls its own write
4. Right after that write the publish method is called, a method on Versioned decorator that calls its owner's write method. "

I have not worked on a fix yet. Anyone has?
Is there maybee a way to find out which function has triggerd the onAfterWrite-Event?

bye henkrid

Avatar
swaiba

Forum Moderator, 1899 Posts

25 August 2011 at 5:46am

I'm not going through the whole thread (so I'm not 100% this will help) but the $obj->write is only good for a plain dataobject, if you want to save AND publish a Page then do this...

$object->writeToStage('Stage'); 
$object->publish('Stage', 'Live');

Avatar
henkrid

Community Member, 2 Posts

25 August 2011 at 8:01pm

Hey swaiba,
thanks for your suggestion.
this works out, but the generated child-page gets directly published on creation.

But i would be nicer if the child-page gets published while publishing the parent-page.
I am working on it but it's a little tricky because the parent-page has different children in different versions.