Cakephp:在组件中使用$ this-> Component-> load()

时间:2014-02-27 13:21:02

标签: cakephp cakephp-2.1

我想在组件中添加组件。

我发现了这个问题

$this->OneTimer = $this->Components->load('OneTimer');
$this->OneTimer->getTime();
(in the fly-mehtod)

但是,当我在我的控制器中尝试时,我得到以下内容:

Fatal error: Call to a member function load() on a non-object

我做错了什么?也许fly-method只适用于控制器,而不适用于组件?

谢谢!

3 个答案:

答案 0 :(得分:3)

您可以使用App::uses导入其他组件中的组件

App::uses('OneTimer', 'Controller/Component');
$this->OneTimer = new OneTimerComponent(new ComponentCollection());
$this->OneTimer->getTime();

答案 1 :(得分:0)

您可以使用APP :: import调用另一个组件中的一个组件的方法。 试试这个

      App::import('component', 'OneTimer');
      $this->OneTimer = new OneTimerComponent();
      $this->OneTimer->getTime();

答案 2 :(得分:-1)

它会像这样工作

 App::uses('Component', 'Controller');
    class CalendarComponent extends Object{
        public $components = array('NotificationManager');
        function test(){
            $this->NotificationManager->callfunction();
        }
    }