更新joomla中的现有文章

时间:2018-05-03 10:33:57

标签: php joomla

我有用于在joomla中存储新文章的代码:

JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table');

$article = JTable::getInstance('content');

$article->title            = 'This is my super cool title!';
$article->alias            = JFilterOutput::stringURLSafe('This is my super cool title!');
$article->introtext        = "some content";
$article->catid            = 9;
$article->created          = JFactory::getDate()->toSQL();;
$article->created_by_alias = 'my editor';
$article->state            = 1;
$article->access           = 1;
$article->metadata         = '{"page_title":"","author":"","robots":""}';
$article->language         = '*';

// Check to make sure our data is valid, raise notice if it's not.
if (!$article->check())
{
    JError::raiseNotice(500, $article->getError());

    return FALSE;
}

// Now store the article, raise notice if it doesn't get stored.
if (!$article->store(TRUE))
{
    JError::raiseNotice(500, $article->getError());

    return FALSE;
}

但我找不到如何使用其ID更新现有的。 文档在哪里?你有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您可以使用load函数首先加载该记录,并在分配新值后将其存储到表中,如下面的代码所示 -

$articleId = 6;// Change this to actual article id
JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table');
$article = JTable::getInstance('content');
$article->load($articleId);
$article->title = $article->title . ' Updated';
if (!$article->check() || !$article->store())
{
    throw new RuntimeException($article->getError());
}