4

I want to add components in an component.

I've found this on the cake

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

But, when I try it in my controller i get the following:

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

What am I doing wrong? Maybe the in the fly-method is only for controllers, and not for components?

Thanks!

cornelb
  • 6,046
  • 3
  • 19
  • 30
Bob
  • 873
  • 1
  • 8
  • 21

3 Answers3

4

You can just use App::uses to import components in another component

App::uses('OneTimer', 'Controller/Component');
$this->OneTimer = new OneTimerComponent(new ComponentCollection());
$this->OneTimer->getTime();
cornelb
  • 6,046
  • 3
  • 19
  • 30
0

You can call a method of one component within another component using APP::import. Try this one

      App::import('component', 'OneTimer');
      $this->OneTimer = new OneTimerComponent();
      $this->OneTimer->getTime();
Smruti
  • 452
  • 4
  • 6
-1

It will work simply like this

 App::uses('Component', 'Controller');
    class CalendarComponent extends Object{
        public $components = array('NotificationManager');
        function test(){
            $this->NotificationManager->callfunction();
        }
    }
Deepend
  • 4,057
  • 17
  • 60
  • 101
  • I think it would be more helpful for the op and further visitors, when you add some explaination to your intension (you can doing so, by using the edit link underneath your post). – Reporter Oct 02 '14 at 13:24
  • it is self explanatory even question is very clear so of course here component is using another component. – Amjad Farooq Oct 10 '14 at 14:05