[英]Programmatically add a Joomla Article from CLI
我希望能夠使用 Joomla CMS 中的 cli 功能從命令行以編程方式在 Joomla 中添加許多文章。
我基本上是在使用Create a Joomla! 文章以編程方式,但我的腳本在僅創建一篇帶有錯誤行的文章后關閉
顯示錯誤頁面時出錯:應用程序實例化錯誤:應用程序實例化錯誤
這是我在 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 ...
我這樣做了,效果很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.