Get it All
Together

For reasons I’m not entirely clear on, CakePHP developers object in general to the practice of including Models into your Componets. I suspect the problem lies in the idea that you might end up with lots and lots of associated Models within the overall framework of a given Controller, leading to bad performance. That makes some sense, but why aren’t there any directions then on how to include the Controller’s Models into the Component, when its actually quite easy to do?

I dunno. But I finally figured it out and I’m passing along the information.

The trick is using the initialize() function. I presume that CakePHP must look at all included Objects to see if this function exists, otherwise I’m not entirely sure how else it becomes a globally recognized function. Perhaps someone more knowledgeable can chime in down there in the comments thingy?

Still, here is some sample code that first includes the Controller into the Component as a reference (taken from this example in the CakePHP Book) and then takes the logical next step of including the Model (in this case, WebPriceView) into the Component as well:
[php]
/*
// initialize: Initializing the Component, importing some needed Objects from the Controller.
*/
public function initialize(&$controller) {
$this->controller =& $controller;
$this->WebPriceView =& $controller->WebPriceView;
}
[/php]
It bears mentioning that this example can be extended well beyond including Models to including helpers and anything else you need from the Controller.