2010-09-15 65 views

回答

4

獲取模型非常簡單。只需在插件代碼中包含來自組件的模型PHP文件並根據需要創建對象。

最好處理模型中的所有表操作,但是有方法可以在插件本身中加載表。

這裏是你如何從加載模型插件:

<?php 

// Path to component 
$componentPath = JPATH_ADMINISTRATOR . DS . 'components' . DS . 'mycomponent'; 

// Include model 
require_once $componentPath . DS . 'models' . DS . 'example.php'; 

// You need to specify table_path because by default model uses 
// JPATH_COMPONENT_ADMINISTRATOR . DS . 'tables' 
// and you will not have correct JPATH_COMPONENT_ADMINISTRATOR in the plu-in 
// unless you specify it in config array and pass it to constructor 
$config = array(
    'table_path' => $componentPath . DS . 'tables' 
); 

// Create instance 
$model = new MycomponentModelExample($config); 

?> 

這裏是你如何從插件加載表:

<?php 

// 1. Add the path so getInstance know where to find the table 
$tablePath = JPATH_ADMINISTRATOR . DS . 'components' . DS . 'mycomponent' . DS . 'tables'; 
JTable::addIncludePath($tablePath); 

// 2. Create instance of the table 
$tbl = JTable::getInstance('tableName', 'Table'); 

?> 
相關問題