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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

[RESOLVED] onBeforePublish returns an error / onBeforeWrite works


Go to End


4 Posts   3495 Views

Avatar
lise

Community Member, 47 Posts

6 December 2011 at 10:08am

Edited: 06/12/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.php

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.

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.

Avatar
lise

Community Member, 47 Posts

7 December 2011 at 9:54am

Edited: 07/12/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.

Avatar
Willr

Forum Moderator, 5523 Posts

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.

Avatar
lise

Community Member, 47 Posts

14 December 2011 at 10:34am

Edited: 14/12/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