I have a controller which I use to run a list of processes through a cron, and I need it to run a method from a separate controller.
When that method is called from its own controller, everything works fine, but when running from the cron controller (even if called manually), it is failing.
My code currently looks something like this:
class CronController extends AppController
{
var $uses = array(.., 'EmailTemplate', ..);
var $components = array('Email');
//Some stuff
function process_for_cron_to_run() {
//Some stuff
//Run method from imported controller
App::import('Controller', 'Imported');
$imported = new ImportedController;
$imported->constructClasses();
$imported->method_to_run(); //Dies when running this method
exit;
}
}
...
class ImportedController extends AppController {
var $uses = array(..);
var $components = array('Mailer', 'RequestHandler', 'FileHandler', 'Email');
//Some stuff
function method_to_run() {
//Some stuff
$this->_sendEmail(..);
}
}
...
class AppController extends Controller
{
var $components = array('Cookie', 'RequestHandler', 'Err', 'Email', 'Sms');
var $uses = array(.., 'EmailTemplate', ..);
function _sendEmail(..) {
//Some stuff
$this->EmailTemplate->find(..); //Dying here (Line 1756)
}
..
}
This is dying with the following error:
Fatal error: Call to a member function find() on a non-object in /../app/app_controller.php on line 1756
Where line 1756 is in the AppController above.
I tried adding EmailTemplate
to the $uses
of the ImportedController - this worked, but it instead died a few lines later in the AppController on a call to $this->Email
, which is included in the $components
of all three controllers. The error message for $this->email
, for reference:
Fatal error: Call to a member function reset() on a non-object in /../app/app_controller.php on line 1645
The only other thing I can think of is that the method I am calling from ImportedController is an admin_X, so when I'm calling it from /processes/process_for_cron_to_run, it could be getting kicked out due to authentication issues?
I have resolved this issue now; there were two steps I had to take to fix it:
Updated the process_for_cron_to_run() to initalise components:
function process_for_cron_to_run() {
//Some stuff
App::import('Controller', 'Imported');
$imported = new ImportedController;
$imported->_initComponents(); //Added this line
$imported->constructClasses();
$imported->method_to_run();
exit;
}
The reason it was dying was because the imported controller didn't have its components initialised; when the object made a call to _sendEmail in the AppController, there were no components attached to it, so it did not know what $this->Email was.
The ideal solution to this problem, however, would have been to move the method_to_run logic into a model or component, rather than importing the controller.