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.
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.
- Previous 10 entries
- 1
- 2
- Page 33(current)
- 4
- Next 10 entries
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 :)
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
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
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();
}
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
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');
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.
- Previous 10 entries
- 1
- 2
- Page 33(current)
- 4
- Next 10 entries