Php 创建一个Joomla!以编程方式撰写文章
我已经创建了自己的组件。当我向组件添加新记录时,我还希望它在joomla中创建一篇新文章(即使用com_内容) 我在stack overflow上发现了这个,它解释了如何执行此操作。代码是有意义的,看起来它会工作。问题在于,一旦开始调用com_内容中包含的方法,com_内容中的所有相对URL都会崩溃,joomla就会抛出一个错误Php 创建一个Joomla!以编程方式撰写文章,php,joomla,content-management-system,joomla2.5,Php,Joomla,Content Management System,Joomla2.5,我已经创建了自己的组件。当我向组件添加新记录时,我还希望它在joomla中创建一篇新文章(即使用com_内容) 我在stack overflow上发现了这个,它解释了如何执行此操作。代码是有意义的,看起来它会工作。问题在于,一旦开始调用com_内容中包含的方法,com_内容中的所有相对URL都会崩溃,joomla就会抛出一个错误 有人知道克服这个问题的方法吗?上面链接中的一条评论表明,在包含当前工作目录之前将其更改为com_内容目录将起作用,但我不能100%确定如何执行此操作。无法更改工作目录,
有人知道克服这个问题的方法吗?上面链接中的一条评论表明,在包含当前工作目录之前将其更改为com_内容目录将起作用,但我不能100%确定如何执行此操作。无法更改工作目录,因为它是一个常量。要解决此问题,您可以选择根本不使用ContentModelArticle,而只使用table类:
$table=JTable::getInstance('Content','JTable',array());
$data=数组(
“catid”=>1,
“标题”=>“某些标题”,
“introtext”=>“一些文本”,
“全文”=>“一些文本”,
“状态”=>1,
);
//绑定数据
如果(!$table->bind($data))
{
$this->setError($table->getError());
返回false;
}
//检查数据。
如果(!$table->check())
{
$this->setError($table->getError());
返回false;
}
//存储数据。
如果(!$table->store())
{
$this->setError($table->getError());
返回false;
}
请注意,上面的代码不会触发保存前/后事件。但是,如果需要,触发这些事件应该不是问题。另外值得注意的是,不会自动设置“已发布”字段,类别内的文章也不会重新排序
要重新排列类别,请执行以下操作:
$table->reorder('catid='(int)$table->catid.'和state>=0');
我收到的错误是:
未找到文件/var/www/administrator/com_mynewcomponent/helpers/content.php
我在这个位置创建了一个空文件来抑制错误消息,并手动使用一个require\u once
语句包含/var/www/administrator/com\u content/helpers/content.php
。支持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;
}
if(版本比较(JVERSION,'3.0','lt')){
JTable::addIncludePath(JPATH_平台'joomla/database/table');
}
$article=JTable::getInstance('content');
$article->title='这是我超级酷的标题!';
$article->alias=JFilterOutput::stringURLSafe('这是我超级酷的标题!');
$article->introtext='这是我的超级酷的文章 ",;
$article->catid=9;
$article->created=JFactory::getDate()->toSQL();
$article->created_by_alias='Super User';
$article->state=1;
$article->access=1;
$article->metadata='{“页面标题”:“,”作者“,”机器人“:”}”;
$article->language='*';
//检查以确保我们的数据有效,如果数据无效,请发出通知。
如果(!$article->check()){
JError::raiseNotice(500美元,$article->getError());
返回FALSE;
}
//现在存储文章,如果没有存储,请发出通知。
如果(!$article->store(TRUE)){
JError::raiseNotice(500美元,$article->getError());
返回FALSE;
}
Joomla抛出的错误是什么?还有,Joomla的哪个版本?我想PHP再清楚不过了。。。文件不存在。是true,因为相对URL未使用正确的基本路径-这是我的问题。$table=JTable::getInstance('Content','JTable',array());在Joomla 2.5中不适用于我,但$table=JTable::getInstance('content','JTable');工作。