2012-06-20 47 views
2

好的,我一直在關注joomla 2.5教程here,並且我設法制作了一個無故障的初始組件。如何初始化我的自定義joomla 2.5模型?

但我想知道如何導入額外的類到框架?

我有一個名爲auth.php模型類

// No direct access to this file 
defined('_JEXEC') or die('Restricted access'); 

// import Joomla modelitem library 
jimport('joomla.application.component.modelitem'); 

/** 
* Auth Model 
*/ 
class AutoBaseModelAuth extends JModelItem 
{ 
    function detail() 
    { 
     echo "this is test"; 
    } 
} 

位於C:/xampp/htdocs/com_autobase/model/auth.php

xampp http://iforce.co.nz/i/p5m1gjxh.jpx.png

而我的看法...

// No direct access to this file 
defined('_JEXEC') or die('Restricted access'); 

// import Joomla view library 
jimport('joomla.application.component.view'); 

/** 
* HTML View class for the AutoBase Component 
*/ 
class AutoBaseViewAutoBase extends JView 
{ 
    // Overwriting JView display method 
    function display($tpl = null) 
    {  
     $db  =& JFactory::getDBO(); 
     //request the auth model 
     $model =& $this->getModel('auth'); 
     $items =& $model->detail(); 
    } 
} 

但我保持gett這個錯誤忘了,因爲它尚未導入......我一直在約5個不同的網站試圖找出Joomla如何導入新模型

Notice: Undefined index: auth in C:\xampp\htdocs\libraries\joomla\application\component\view.php on line 413 

所以,可以有人請解釋模型如何在初始化的Joomla?和我做錯了什麼..謝謝!

+0

的Joomla的版本得到一個模型的任何地方? – Craig

+0

@cppl剛剛更新了問題標題。 Joomla Version 2.5 – Killrawr

回答

6

我們通常在我們包括所有組件的幫助這個靜態函數

public static function getModel($name, $component_name = null, $config = array()) { 

    //Use default configured component unless other component name supplied 
    if(!$component_name) { 
     $component_name = self::$com_name; 
    } 

    jimport('joomla.application.component.model'); 
    $modelFile = JPATH_SITE . DS . 'components' . DS . $component_name . DS . 'models' . DS . $name.'.php'; 
    $adminModelFile = JPATH_ADMINISTRATOR . DS . 'components' . DS . $component_name . DS . 'models' . DS . $name.'.php'; 
    if (file_exists($modelFile)) { 
     require_once($modelFile); 
    } elseif (file_exists($adminModelFile)) { 
     require_once($adminModelFile); 
    } else { 
     JModel::addIncludePath(JPATH_SITE . DS . 'components' . DS . $component_name . DS . 'models'); 
    } 

    //Get the right model prefix, e.g. UserModel for com_user 
    $model_name = str_replace('com_', '', $component_name); 
    $model_name = ucfirst($model_name).'Model'; 

    $model = JModel::getInstance($name, $model_name, $config); 

    return $model; 
} 

然後,您可以去

$model = helper::getModel('Name', 'ComponentName'); 
+1

感謝回答救生員:) – Killrawr

+0

我可以從我的jview調用這個嗎? – jpganz18