希望有一个简单的问题:我有一个插件,它使用了一组表(kb_items,kb_item_tags等)。我希望能够从另一个控制器(比如我的Pages控制器)访问这些模型,因此:
class PagesController extends AppController{
function knowledgebase(){
$items = $this->KbItem->findAll(...);
}
}
我承认我违反了一些规则(因为没有将这个控制器放在知识库插件中),但在这种情况下,这是一个自定义页面,不需要成为知识库插件代码库的一部分。
如果你需要更多的细节,请告诉我。提前感谢您的帮助!
发布于 2010-05-04 02:29:36
我必须自己做这件事,把模型名放在'Uses‘数组中就行了。如果不需要在多个控制器操作中访问模型,还可以使用loadModel()只在需要的操作中访问它。例如,假设您只需要在给定控制器的view()操作中访问此模型:
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');
}
希望这能有所帮助。
发布于 2010-04-30 05:39:36
不确定它在1.1中是否像这样工作,但在1.2+中,您需要在模型名称前加上插件名称和控制器的uses数组中的句号:
class PagesController extends AppController
{
var $uses = array('Page','Kb.KbItem');
function knowledgebase()
{
// This now works
$items = $this->KbItem->findAll();
}
}
发布于 2010-04-29 05:29:21
只需将模型添加到控制器的$uses
属性:
class PagesController extends AppController
{
var $uses = array('Page','KbItem');
function knowledgebase()
{
// This now works
$items = $this->KbItem->findAll();
}
}
https://stackoverflow.com/questions/2731629
复制相似问题