tags:

views:

77

answers:

3

Hopefully a simple question: I've a plugin which uses a set of tables (kb_items, kb_item_tags, etc). and I'd like to be able to access these models from another controller (say, my Pages controller), thus:

class PagesController extends AppController{

function knowledgebase(){
  $items = $this->KbItem->findAll(...);
}

}

I am admittedly breaking the rules a little (by not placing this controller inside the knowledge base plugin), but this in this case its a custom page that doesn't need to be part of the knowledge base plugin code base.

Please let me know if you need more details. Thanks in advance for any help!

A: 

Just add the models to your controllers' $uses property:

class PagesController extends AppController
{
    var $uses = array('Page','KbItem');

    function knowledgebase()
    {
         // This now works
         $items = $this->KbItem->findAll();
    }
}
Daniel Wright
A: 

Not sure if it works like this in 1.1 but in 1.2+ you prefix the model name with the plugin name and a period in the controller's uses array:

class PagesController extends AppController
{
    var $uses = array('Page','Kb.KbItem');

    function knowledgebase()
    {
         // This now works
         $items = $this->KbItem->findAll();
    }
}
neilcrookes
+1  A: 

I just had to do this myself, and putting the model name in the 'Uses' array does work. If you don't need to access the model in multiple controller actions, you can also use loadModel() to access it in just the actions you need. For example, let's say you only need to access this model in the view() action of a given controller:

function view() {
  // load the model, making sure to add the plug-in name before the model name
  // I'm presuming here that the model name is just 'Item', and your plug-in is called 'Kb'
  $this->loadModel('Kb.Item');

  // now we can use the model like we normally would, just calling it 'Item'
  $results = $this->Item->find('all');
  }

Hope that helps.

Walt