dtf0925 2012-10-18 03:45
浏览 80
已采纳

CakePHP导入控制器范围问题

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?

  • 写回答

1条回答 默认 最新

  • doucai9270 2012-10-18 22:10
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题