Answers:
打开模型文件,并在模型类中添加以下行:
public function getContentTable($type = 'Content', $prefix = 'JTable', $config = array())
{
return JTable::getInstance($type, $prefix, $config);
}
现在,您可以在模型类中定义一个方法来添加文章。像这样:
public function addArticle()
{
$table = $this->getContentTable();
$table->title = "Foo";
$table->alias = "foo";
// or
// $table->alias = JApplication::stringURLSafe($table->title);
$table->catid = 2;
$table->state = 1;
// and so on!
// then save it
$table->save();
}
我还不得不以非常规的方式加载文章。我能够利用很多Joomla代码。您需要根据需要进行调整。
该函数将返回给定ID(数字)或别名的article。
function loadArticle($id){
$app = JFactory::getApplication();
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$selects = array('a.introtext','a.publish_up','a.publish_down');
$query->select($selects);
$query->from('#__content as a');
// select the alias or id
$where = 'a.title = ' . $db->q(NNText::html_entity_decoder($id));
$where .= ' OR a.alias = ' . $db->q(NNText::html_entity_decoder($id));
if (is_numeric($id)) {
$where .= ' OR a.id = ' . $id;
}
$query->where('(' . $where . ')');
// check the publish and unpublish dates
$now = JFactory::getDate('now','UTC');
$nullDate = $db->getNullDate();
$query->where('a.state = 1');
$query->where('( a.publish_up = ' . $db->q($nullDate) . ' OR a.publish_up <= ' . $db->q($now) . ' )');
$query->where('( a.publish_down = ' . $db->q($nullDate) . ' OR a.publish_down >= ' . $db->q($now) . ' )');
$db->setQuery($query);
$article = $db->loadObject();
return $article;
}