我希望能够在Joomla中以编程方式添加许多文章,从命令行使用Joomla CMS中的cli功能。
我基本上使用Create a Joomla!文章编程,但我的脚本关闭后,只创建了一篇文章与错误行
显示错误页面的错误:Application Instantiation错误:Application Instantiation Error
这是我在Joomla/cli文件夹中运行的代码。
我正在使用Joomla 3.4
<?php
const _JEXEC = 1;
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_once dirname(__DIR__) . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__DIR__));
require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_LIBRARIES . '/import.legacy.php';
require_once JPATH_LIBRARIES . '/cms.php';
require_once JPATH_CONFIGURATION . '/configuration.php';
class AddArticle extends JApplicationCli
{
public function doExecute()
{
$count = 10;
while ($count > 0)
{
$count--;
$jarticle = new stdClass();
$jarticle->title = 'New article added programmatically' . rand();
$jarticle->introtext = '<p>A programmatically created article</p>';
$table = JTable::getInstance('content', 'JTable');
$data = (array)$jarticle;
// Bind data
if (!$table->bind($data))
{
die('bind error');
return false;
}
// Check the data.
if (!$table->check())
{
die('check error');
return false;
}
// Store the data.
if (!$table->store())
{
die('store error');
return false;
}
}
}
}
JApplicationCli::getInstance('AddArticle')->execute();
我能够找到这个问题的答案,因为它已经在github上提出了一个问题,所以我在这里发布了这个解决方案。
https://github.com/joomla/joomla-cms/issues/7028如果命令行应用程序使用JTable,则必须像这样注册应用程序:
class MakeSql extends JApplicationCli
{
public function __construct()
{
parent::__construct();
JFactory::$application = $this; // this is necessary if using JTable
}
public function doExecute()
{
$db = JFactory::getDbo();
// ... etc etc ...