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.

General Questions

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

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

[RESOLVED] onBeforePublish returns an error / onBeforeWrite works


Reply

4 Posts   2159 Views

Avatar
lise

6 December 2011 at 10:08am (Last edited: 6 December 2011 10:20am), Community Member, 47 Posts

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

7 December 2011 at 9:54am (Last edited: 7 December 2011 10:00am), Community Member, 47 Posts

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

13 December 2011 at 9:42pm Forum Moderator, 5511 Posts

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

14 December 2011 at 10:34am (Last edited: 14 December 2011 10:35am), Community Member, 47 Posts

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