Ken Muldrew Posted September 7, 2018 Posted September 7, 2018 I'm trying to get a short routine to run once per day that will look at some pages and send a reminder email when that customer's subscription (yearly) is about to expire. When I run the code in a template then it works without issue, but inside my lazycron service routine, I get an "Error: Uncaught Error: Call to a member function get() on null" as if the database cannot be found. My autoload module is just the sample HelloWorld module included with ProcessWire, editted to perform this task. The whole of it is included below (I've stripped out the code that generates the email because it never gets past $pages->find): <?php namespace ProcessWire; /** * ProcessWire 'LazyCronLoad' module * */ class LazyCronLoad extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'LazyCronLoad', 'version' => 1, 'summary' => 'Just loads a lazy cron callback.', 'singular' => true, 'autoload' => true, ); } public function init() { // initialize the hook in the AutoLoad module $this->addHook('LazyCron::everyDay', $this, 'myHook'); } public function myHook(HookEvent $e) { // called once per day wire('log')->save('user_activities',' lazy cron service routine'); $transport_pages = $pages->find("template=aggregate-entry, aggregate_type.title='Transport'"); foreach ($transport_pages as $page) { if (($page->purchase_date + 30325800 < time()) && ($page->purchase_date + 30412600 > time())) { // between 351 and 352 days wire('log')->save('user_activities', $page->id . ' email reminder sent'); // send email } } } } The first wire('log') shows up but the second one doesn't (the purchase_date condition is met (as demonstrated by running the code in a template close in time to when the lazycron routine executes)). The error log gives the Uncaught Error shown above. I think this is a beginner's mistake with something obvious being missed and would be grateful for any assistance in fixing it.
Ken Muldrew Posted September 7, 2018 Author Posted September 7, 2018 This is the line where it crashes: $transport_pages = $pages->find("template=aggregate-entry, aggregate_type.title='Transport'"); Do you mean it should be this instead?: $transport_pages = wire('$pages')->find("template=aggregate-entry, aggregate_type.title='Transport'");
psy Posted September 7, 2018 Posted September 7, 2018 Yes, maybe the $pages object isn't set yet ? but no $ sign before pages... wire('pages')
psy Posted September 7, 2018 Posted September 7, 2018 The API vars, $pages, $log, $sanitizer, $fields, etc aren't automatically instantiated inside custom functions. You may want to start your function eg like: $log = wire('log'); $pages = wire('pages'); ... then you can use them as normal 1
teppo Posted September 8, 2018 Posted September 8, 2018 Just a quick heads-up: I'm moving this thread to the "Module/Plugin Development" subforum. The main Modules/Plugins area is intended for support threads of existing modules, while questions regarding module development belong to said subarea ? 2
Ken Muldrew Posted September 10, 2018 Author Posted September 10, 2018 That did the trick, psy. Thank you for your help. 1
dotnetic Posted September 10, 2018 Posted September 10, 2018 On 9/8/2018 at 12:16 AM, psy said: $log = wire('log'); $pages = wire('pages'); If your module is namespaced on top of your .module file <?php namespace ProcessWire; you can use in your functions $this->log // or $this->pages 2
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now