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   8765 Views

Avatar
Sean

Forum Moderator, 922 Posts

21 March 2009 at 1:04pm

Edited: 21/03/2009 1:04pm

That's because there's no check to see if there are children of the page or not, before creating the children.

Your code should probably look something like this instead:


function onAfterWrite() {
	if(!$this->Children()) {
		$cp = new ProgramPage(); 
		$cp->ParentID = $this->ID; 
		$cp->Title = "Test Page";
		$cp->write(); 
	}
	return parent::onAfterWrite(); 
}

Avatar
theAlien

Community Member, 131 Posts

22 March 2009 at 4:46am

Edited: 22/03/2009 4:49am

Hmmm... something seems to be wrong...

I'm using SS2.3.0 and added Persons.php and FormContact.php to mysite/code.
In Persons.php I added

class Persons_Controller extends Page_Controller {

function onAfterWrite() {
   if(!$this->Children()) {
      $cp = new FormContact();
      $cp->ParentID = $this->ID;
      $cp->Title = "Contactform";
      $cp->write();
   }
   return parent::onAfterWrite();
}

}

Next I did run a db/build just to be sure.

After that I logged in to the CMS and choose Persons in the pagetype-dropdown and clicked Go.
However, only a persons-page is created, without any contactform-subpage.
I also tried it without if(!$this->Children()){}, but with no luck either.

Am I making a mistake or am I expecting the wrong behaviour of onAfterWrite() and should I do something else to accomplish this?

Avatar
Carbon Crayon

Community Member, 598 Posts

23 March 2009 at 11:16pm

Edited: 23/03/2009 11:16pm

Hi Alien

The onAfterWrite() function has to go in your model class, not your controller :)

Avatar
snaip

Community Member, 181 Posts

23 March 2009 at 11:42pm


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

   static $allowed_children = array('ProgramPage');
 
   function onAfterWrite() {
      if(!$this->Children()) {  
       
      $cp = new ProgramPage();
      $cp->ParentID = $this->ID;
      $cp->Title = "New day";
      $cp->write();
 
     } 
    return $this->onAfterWrite(); 
   }
   
   
}

dosn't create any page, GO button dosen't work

Avatar
Carbon Crayon

Community Member, 598 Posts

23 March 2009 at 11:48pm

you need to return parent::onAfterWrite(); not $this->onAfterWrite();

Avatar
snaip

Community Member, 181 Posts

24 March 2009 at 12:12am

now i have the same problem like theAlien, only pageholder is created without subpage

Avatar
Carbon Crayon

Community Member, 598 Posts

26 March 2009 at 5:19am

Hi Snaip

For some reason $this->children() is returning true even when there are no children. So I added a count on the end and it seems to work now, so my if statement looks like this:

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

}

hope that helps :)

Avatar
snaip

Community Member, 181 Posts

26 March 2009 at 9:55pm

it works very good now :)
thanks a lot :))