开发者

How to access a plugin model from a regular controller in PHP Cake 1.1

Hopefully a simple question: I've a plugin which uses a set of tables (kb_items, kb_item_tags, etc). and开发者_Go百科 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!


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.


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();
    }
}


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();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜