Create a Joomla! Article Programmatically

做~自己de王妃 提交于 2019-11-27 20:20:15

It's not possible to change the working directory because its a constant. To work around this issue you could choose not to use ContentModelArticle at all and instead use the table class only:

$table = JTable::getInstance('Content', 'JTable', array());

$data = array(
    'catid' => 1,
    'title' => 'SOME TITLE',
    'introtext' => 'SOME TEXT',
    'fulltext' => 'SOME TEXT',
    'state' => 1,
);

// Bind data
if (!$table->bind($data))
{
    $this->setError($table->getError());
    return false;
}

// Check the data.
if (!$table->check())
{
    $this->setError($table->getError());
    return false;
}

// Store the data.
if (!$table->store())
{
    $this->setError($table->getError());
    return false;
}

Note that the code above does not trigger the before/after save events. If that is needed however, it should not be a problem to trigger those events. Also worth noticing is that the field published_up will not be automatically set and the articles within the category will not be reordered.

To reorder the category:

 $table->reorder('catid = '.(int) $table->catid.' AND state >= 0');
Quayph

The error I get says:

File not found /var/www/administrator/com_mynewcomponent/helpers/content.php

I got around the problem by creating an empty file at this location to suppress the error message and manually including /var/www/administrator/com_content/helpers/content.php with a require_once statement.

Frahim

Support Joomla 2.5 and Joomla 3.0

JTableContent is not autoloaded prior to Joomla! version 3.0, so it needs to included:

if (version_compare(JVERSION, '3.0', 'lt')) {
    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        = '<p>This is my super cool article!</p>';
$article->catid            = 9;
$article->created          = JFactory::getDate()->toSQL();
$article->created_by_alias = 'Super User';
$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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!