CakePHP how to use Model in Component

I know it’s against the main design stream of CakePHP applications to use models in the components and I was restricting myself from doing that… but finally I decide to make a try and see will that simplify my CakePHP applications development.
Let me explain a bit my motivation to use model in component. I want to add some controller output independent blocks to various screens of my application, e.g. to show list of last posts on screen 1,2 and 3. Currently I pass data from controller to layout component, so each controller method has almost the same part of code to load data from model, process data an put in to the session, so later the view element can render it. On the other hand if the data load, processing and session manipulation will be in the component then the only thing left for controller method is to add component in to the used components list.
So, here is the solution. The core controller loads models with Controller::loadModel() method. And here is how we can reuse that functionality in the component:

<?php 
class BaseModelComponent extends Object {
 
	var $uses = false;
 
	function initialize(&$controller) {
 
		//load required for component models
		if ($this->uses !== false) {
			foreach($this->uses as $modelClass) {
				$controller->loadModel($modelClass);
				$this->$modelClass = $controller->$modelClass;
			}
		}
 
	}
 
}
?>

As you can see from its name this is a base class. So if you would like to use a model in your component just inherit it from the BaseModelComponent, like this:

<?php 
App::import('Component', 'BaseModel'); 
class MyFooComponent extends BaseModelComponent {
 
	var $uses = array('Foo'); //models to load
 
	function initialize(&$controller) {
		return parent::initialize($controller); //important to initialize base component
	}
 
	function doFoo() {
		debug($this->Foo->findAll()); //sample output to test it works
	}
}
?>

After your base class changed, the Foo component can define in the $uses variable list of the models to use. Now you can access the model the same way as you’ve done that in the controller. Controller code can be moved to the component without any change.

Hope this helps. Have a nice day.