我在Joomla 2.5
. 在这个组件中,我想获取所有可用的用户com_users
。为此,我想让你知道,我如何在我的组件中使用com_users
模型类。任何人都有如何做到这一点的建议。
问问题
3107 次
2 回答
3
根据您想在哪里使用模型,您可以简单地询问 Joomla!为您加载它。
在JController
类或子类getModel
中,您可以调用传入模型名称和组件前缀...
例如
JModel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_users/models/');
$model = $this->getModel($name = 'User', $prefix = 'UsersModel');
可能需要添加要加载的外部模型的路径,JModel::addIncludePath()
如上所示。
或者,如果您确定模型名称和类前缀,您可以使用JModel
'sgetInstance()
创建所需的模型对象...例如
$model = JModel::getInstance('User', 'UsersModel');
或者,在视图中,您可以:
$myModel = $this->getModel('myOtherModel');
$this->setModel($myModel);
注意在第一行中,我们传递了我们想要的模型名称,通常您调用时getModel
不带任何参数来加载组件视图控制器的默认模型。在第二行中,由于我们只是将模型传递给setModel()
它,因此不会使其成为视图使用的默认模型。
当我们稍后想要使用我们的模型对象时,我们可以像这样指定我们想要使用的对象:
$item = $this->get('Item');
$otherItem = $this->get('Item', 'myOtherModel' );
第一行使用视图的默认模型(因为我们在可选参数中指定了一个)。第二行使用getItem()
from myOtherModel
。
这一切都有效,因为JView
(in 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;
}
于 2013-01-07T12:13:11.757 回答
1
尝试这样的事情
if(!class_exists('UsersModelUser')) require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_users'.DS.'models'.DS.'user.php');
您可以从管理员端或前端添加模型的正确路径。
VM2.x 组件就是这样使用的。
或者您只需要有关您可以使用的用户的一些详细信息。
$user = JFactory::getUser();
希望这可以帮助你..
于 2013-01-07T10:56:15.307 回答