Our application is an online booking system. Once a day we want to run a cron job to send out reminder emails if someone’s account becomes due; we would like those emails to be nicely formatted in HTML. We have an emailer class that is extended from CController that we use for sending emails based on events, e.g. creating a booking, and this allows us to use controller rendering, layouts, etc. which allows us to nicely keep content and layout separated.
Running a cron job seems to be a job for a console application. Problem with this is that when it comes to rendering the emails Yii::app()->getTheme is called. Yii::app() being a console app doesn’t have the getTheme method (it’s in CWebApplication).
We could of course run the cron jobs as web apps, but we think that this makes them available to the world - something we don’t want for obvious reasons.
Have we missed something? Any help to achieve what we want to do is appreciated.
I’m not sure about this, but looking at the implementation of getTheme() in CWebApplication, I think you can create an instance of CThemeManager or extend ConsoleApplication with a themeManager core component, a _theme property and the methods getTheme()/setTheme().
Thanks to Tommy for the reply; it got us thinking.
We have got this working, but it feels like a cludge - the first time that’s happened with Yii, and we still wonder if we are doing it the best way.
What we have done is write a Yii bootstrap for the console app that overrides YiiBase::createConsoleApplication. This returns our EConsoleApplication that is extended from CConsoleApplication.
In EConsoleApplication we have put the methods and properties from CWebApplication we need to get things to work; namely:
Hi - has anyone been able to figure out how to accomplish the above. I am extending the right classes and overriding, but Yii::app()->theme always has a NULL reference.
Ok, so now I figured out how to wire in and access Yii::app()->theme from within the console application - question now is how do we go about GENERATING the html as we don’t have access to the render/renderPartial methods of CController?
I want to basically be able to pass in dynamic data to the view and the view styles it in a nice format and the rendering engine returns the ENTIRE html contents which I just push to the end user via PHP’s native mail API.