【问题标题】:Create a Joomla! Article Programmatically创建一个 Joomla!以编程方式撰写文章
【发布时间】:2012-09-20 13:23:25
【问题描述】:

我创建了自己的组件。当我向我的组件添加一条新记录时,我还希望它在 joomla 中创建一篇新文章(即使用 com_content)。

我在堆栈溢出Programmatically adding an article to Joomla 上找到了这个,它解释了如何做到这一点。该代码很有意义,并且看起来可以工作。问题是一旦开始调用 com_content 中包含的方法,com_content 中的所有相对 URL 都会崩溃,并且 joomla 会抛出错误。

有谁知道克服这个问题的方法吗?来自上述链接的评论表明,在包含它之前将当前工作目录更改为 com_content 会起作用,但我不能 100% 确定如何执行此操作。

【问题讨论】:

  • Joomla 抛出的错误是什么?另外,Joomla 是什么版本的?
  • 我觉得PHP再清楚不过了……文件不存在。
  • 是的,因为相对 URL 没有使用正确的基本路径 - 这是我的问题。

标签: php joomla content-management-system joomla2.5


【解决方案1】:

无法更改工作目录,因为它是一个常量。要解决此问题,您可以选择根本不使用 ContentModelArticle,而只使用表类:

$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;
}

请注意,上面的代码不会触发之前/之后的保存事件。但是,如果需要,触发这些事件应该不是问题。另外值得注意的是,字段published_up不会自动设置,类别内的文章也不会重新排序。

要重新排序类别:

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

【讨论】:

  • $table = JTable::getInstance('Content', 'JTable', array());在 Joomla 2.5 中对我不起作用,但 $table = JTable::getInstance('content', 'JTable');工作。
【解决方案2】:

我得到的错误是:

找不到文件 /var/www/administrator/com_mynewcomponent/helpers/content.php

我通过在此位置创建一个空文件来抑制错误消息并手动将/var/www/administrator/com_content/helpers/content.php 包含在require_once 语句中,从而解决了这个问题。

【讨论】:

    【解决方案3】:

    支持 Joomla 2.5 和 Joomla 3.0

    JTableContent 在 Joomla! 之前不会自动加载! 3.0版本,所以需要包含:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-10
      • 2011-10-25
      • 1970-01-01
      • 2012-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      相关资源
      最近更新 更多