我正在为 Joomla 2.5 创建一个自定义组件。我想为详细视图添加命中计数器(如 Joomla 内容等)
所以我做了一些研究,发现JTable::hit
我假设的功能是我需要的(Joomla docs page here)。但是,我非常不清楚我需要在哪里实现这一点。当我从数据库中检索行时,它是在模型中的某个地方 - 还是在视图中的某个地方?建议将不胜感激!
如果您看看他们在 Joomla 中是如何做到的,您会看到他们在模型中实现了功能,并从视图中调用了方法。
例如,article.php
模型有
public function hit($pk = 0)
{
$hitcount = JRequest::getInt('hitcount', 1);
if ($hitcount)
{
// Initialise variables.
$pk = (!empty($pk)) ? $pk : (int) $this->getState('article.id');
$db = $this->getDbo();
$db->setQuery(
'UPDATE #__content' .
' SET hits = hits + 1' .
' WHERE id = '.(int) $pk
);
if (!$db->query()) {
$this->setError($db->getErrorMsg());
return false;
}
}
return true;
}
和
view.html.php
拥有
$model = $this->getModel();
$model->hit();
您可以使用表类。在 Joomla 3 和 Joomla 4 中工作。
你的模型
use Joomla\CMS\Table\Table;
public function hit($id = 0)
{
Table::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_mycomp_name/tables');
$table = Table::getInstance('my_table_name', 'Table', array());
$table->hit($id);
return true;
}
你的view.html.php
$model = $this->getModel('model_name');
$model->hit($id);
您可以使用它来增加任何列。为此,您需要覆盖该列。
$table->setColumnAlias('hits', 'new_column_name');
$table->hit($id);