21309 Posts in 5738 Topics by 2603 members
General Questions
SilverStripe Forums » General Questions » [RESOLVED] onBeforePublish returns an error / onBeforeWrite works
General questions about getting started with SilverStripe that don't fit in any of the categories above.
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 1254 Views |
-
[RESOLVED] onBeforePublish returns an error / onBeforeWrite works

6 December 2011 at 10:08am Last edited: 6 December 2011 10:20am
I am trying to have an email sent every time a page is published. I am thinking of using onBeforePublish() for that.
To test it, I created a new page class :
class PageEmail extends Page {
function onBeforePublish() {
parent::onBeforePublish();
$subject = 'Page published '.$this->Title;
$email = new Email();
$email->setTo("myemail@yahoo.com");
$email->setFrom( Email::getAdminEmail());
$email->setSubject($subject);
$email->send();
}
}When I try to "publish" a page of this type , I get the error message :
Error: Uncaught Exception: Object->__call(): the method 'onbeforepublish' does not exist on 'PageEmail'
At line 724 in /public_html/mydomain/silverstripe/sapphire/core/Object.phpNote that if I replace "onBeforePublish" with "onBeforeWrite" in the class definition, I get the email *twice* even though the function is not in page.php.
I also noticed that the function name in the error message is all in lower cases but I don't know if this is relevant.
I am obviously doing something wrong. Could you please point me in the right direction? ( I run version 2.4 of SilverStripe )
Thank you.
-
Re: [RESOLVED] onBeforePublish returns an error / onBeforeWrite works

7 December 2011 at 9:54am Last edited: 7 December 2011 10:00am
I don't seem to get a lot of help on this one....
For those interested, onAfterPublish() seems to work but I still don't get why onBeforePublish() does not.
Any idea? Tip? Example?
Cheers.
-
Re: [RESOLVED] onBeforePublish returns an error / onBeforeWrite works

13 December 2011 at 9:42pm
Note that if I replace "onBeforePublish" with "onBeforeWrite" in the class definition, I get the email *twice* even though the function is not in page.php.
When you publish a page it requires 2 writes by the ORM.
onBeforePublish() is different from onBeforeWrite() in that onBeforePublish is a extension (mix-in) function so parent::onBeforePublish() won't refer to anything in this case. Try remove that reference and include the parameter $object as per the function call definition.
-
Re: [RESOLVED] onBeforePublish returns an error / onBeforeWrite works

14 December 2011 at 10:34am Last edited: 14 December 2011 10:35am
Thank you Willr for resolving this mystery.
The problem was with the call to parent::onBeforePublish(). I didn't call the parent method when I tried onAfterPublish and
didn't imagine it could be the cause of the problem. Both functions have the same signature and both work in the above code ( once the call to parent::... is removed )Maybe I should mention that I placed the function (onBeforePublish / onAfterPublish ) in the class (not in the controller) because I vaguely remember somebody on this Forum mentioning that it would not work in the Controller (sorry I didn't bookmark it)
Thanks again
| 1254 Views | ||
|
Page:
1
|
Go to Top |


