2013-01-07 40 views

回答

3

根据您要使用的模型,你可以简单地问的Joomla!为你加载它。

JController类或子类,你可以调用getModel通过在型号名称和成分前缀......

例如

JModel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_users/models/'); 
$model = $this->getModel($name = 'User', $prefix = 'UsersModel'); 

可能需要添加要使用以上作为JModel::addIncludePath()显示加载外部模型的路径。

或者,如果你确定型号名称和类的前缀,你可以使用JModelgetInstance()以创建所需的模型对象...例如

$myModel = $this->getModel('myOtherModel'); 
$this->setModel($myModel); 

注::

$model = JModel::getInstance('User', 'UsersModel'); 
在视图中你可以

或者在第一行中,我们传递了我们想要的模型名称,通常您不需要任何参数即可调用getModel来加载组件视图控制器的默认模型。在第二行中,因为我们只将模型传递给setModel(),它不会使其成为视图使用的默认模型。

当我们想用我们的模型对象,稍后我们可以指定我们要使用这样的:

$item = $this->get('Item'); 
$otherItem = $this->get('Item', 'myOtherModel'); 

第一行使用视图的默认模式(因为我们已经指定了一个在选购参数)。第二行使用myOtherModelgetItem()

这是所有的作品,因为JView(在libraries/joomla/application/view.php)有以下方法:

/** 
* Method to get the model object 
* 
* @param string $name The name of the model (optional) 
* 
* @return mixed JModel object 
* 
* @since 11.1 
*/ 
public function getModel($name = null) 
{ 
    if ($name === null) 
    { 
     $name = $this->_defaultModel; 
    } 
    return $this->_models[strtolower($name)]; 
} 

/** 
* Method to add a model to the view. We support a multiple model single 
* view system by which models are referenced by classname. A caveat to the 
* classname referencing is that any classname prepended by JModel will be 
* referenced by the name without JModel, eg. JModelCategory is just 
* Category. 
* 
* @param JModel &$model The model to add to the view. 
* @param boolean $default Is this the default model? 
* 
* @return object The added model. 
* 
* @since 11.1 
*/ 
public function setModel(&$model, $default = false) 
{ 
    $name = strtolower($model->getName()); 
    $this->_models[$name] = &$model; 

    if ($default) 
    { 
     $this->_defaultModel = $name; 
    } 
    return $model; 
} 
+0

所以它也适用于使用其他组件的模型类? – Toretto

+0

查看我更新的答案,了解加载特定模型的几种替代方法。 – Craig

+0

对不起,但它根本不工作。 – Toretto

0

尝试这样的事情

if(!class_exists('UsersModelUser')) require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_users'.DS.'models'.DS.'user.php'); 

您可以从管理方或前端添加正确的路径模型。

VM2.x组件正在以这种方式使用。

或者您只需要一些关于您可以使用的用户的详细信息。

$user = JFactory::getUser(); 

希望这可以帮助你..

+0

我知道,由包含这个文件,我可以使用它的所有功能,但是我不想用'要求'。是否有任何Joomla方法或插件,这将有所帮助。 – Toretto

+0

只有需要或包含核心文件的功能,如果你想包含库,那么你可以使用jimport() –

相关问题