Create methods in news, events, and link models to get the last 5 entries. Then, in your controller, models in the Controller :: uses property are turned on, or in action use ClassRegistry :: init () to access the model, for example.
function my_action() { $news = ClassRegistry::init('News')->getRecent(); $events = ClassRegistry::init('Event')->getRecent(); $links = ClassRegistry::init('Link')->getRecent(); $this->set(compact('news', 'events', 'links')); }
You can then call these model methods from any controller action while saving the DRY application.
In your view my_action.ctp and even in many other views just draw the elements, for example.
// my_action.ctp <?php echo $this->element('recent_news'); echo $this->element('recent_events'); echo $this->element('recent_links'); ?>
Then your elements can simply iterate over the $ news variable (or any other) that displays elements with links to "view" actions in their respective controllers.
Just because the controller matches the model does not mean that you cannot use other models in it.
source share