3

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
  • 1
    I have resolved this issue; I had to initialise the components with an $imported->_initComponents() call in the process_for_cron_to_run function. It's strange that I had to do this, as 'Email' was already included in the $uses for the CronController, but it resolved the issue. I will post a proper answer for this once I'm allowed! – Jarred Mack Oct 18 '12 at 04:53
  • Please consult [this post](http://stackoverflow.com/a/12769983/727208) – tereško Oct 18 '12 at 09:55
  • 2
    if you have some code that needs to be executed from two (or more) different controllers, then that code needs to be inside a component. Of course it might take you a few more minutes, but your code will be cleaner. – pleasedontbelong Oct 18 '12 at 12:25
  • @Moruk controller initialization could change in the future, you should really heed the advice above and separate the logic out into a component. – jeremyharris Oct 18 '12 at 14:06
  • @pleasedontbelong I agree, and if this logic was in the model in the first place I wouldn't have had an issue. I didn't write this function, though, and the logic was too intertwined with the rest of the controller (there are a lot of internal calls to other functions within the class), and I did not have the time to move it all to a component and test it was working correctly. – Jarred Mack Oct 18 '12 at 22:14

1 Answers1

-1

I have resolved this issue now; there were two steps I had to take to fix it:

  1. Add 'EmailTemplate' to the $uses so the ImportedController could reference it. This meant that the code now dies when trying to call $this->Email.
  2. 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.