5095 Posts in 1518 Topics by 1114 members
| Go to End | Next > | |
| Author | Topic: | 3410 Views |
-
Re: Automatically Creating Child Pages

21 March 2009 at 1:04pm Last edited: 21 March 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();
} -
Re: Automatically Creating Child Pages

22 March 2009 at 4:46am Last edited: 22 March 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 addedclass 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?
-
Re: Automatically Creating Child Pages

23 March 2009 at 11:16pm Last edited: 23 March 2009 11:16pm
Hi Alien
The onAfterWrite() function has to go in your model class, not your controller
-
Re: Automatically Creating Child Pages

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
-
Re: Automatically Creating Child Pages

23 March 2009 at 11:48pm
you need to return parent::onAfterWrite(); not $this->onAfterWrite();
-
Re: Automatically Creating Child Pages

24 March 2009 at 12:12am
now i have the same problem like theAlien, only pageholder is created without subpage
-
Re: Automatically Creating Child Pages

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
-
Re: Automatically Creating Child Pages

26 March 2009 at 9:55pm
it works very good now
thanks a lot
)
| 3410 Views | ||
| Go to Top | Next > |



