JTableCategory used in JApplicationCli like in ExampleApp in code below.
Categories should be added.
Error occurs when trying to store the second category:
Application Instantiation Error caused by line 890 in libraries/joomla/table/nested.php:
$this->_observers->update('onAfterStore', array(&$result));
(If I remove the line 890, the insertion works.)
PHP 5.4.36-0+deb7u3
Joomla! 3.4.1
*************************/
// Make sure we're being called from the command line, not a web interface
if (PHP_SAPI !== 'cli')
{
die('This is a command line only application.');
}
// We are a valid entry point.
const _JEXEC = 1;
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 1);
// Load system defines
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';
// Load the configuration
require_once JPATH_CONFIGURATION . '/configuration.php';
// ----------------------
class InsertCatExampleCli extends JApplicationCli {
public function __construct() {
parent::__construct();
$this->config = new JConfig();
$this->dbo = JDatabase::getInstance(
array(
'driver' => $this->config->dbtype,
'host' => $this->config->host,
'user' => $this->config->user,
'password' => $this->config->password,
'database' => $this->config->db,
'prefix' => $this->config->dbprefix,
)
);
}
public function doExecute() {
$category_titles = array("Test Title 1", "Test Title 2", "Test Title 3");
// add categories to database
for ($i=0; $i<count($category_titles); $i++) {
$this->insertCategory($category_titles[$i]);
}
}
private function insertCategory($cat_title) {
$tbl_cat = JTable::getInstance("Category");
// get existing aliases from categories table
$tab = $this->dbo->quoteName($this->config->dbprefix . "categories");
$conditions = array(
$this->dbo->quoteName("extension") . " LIKE 'com_content'"
);
$query = $this->dbo->getQuery(true);
$query
->select($this->dbo->quoteName("alias"))
->from($tab)
->where($conditions);
$this->dbo->setQuery($query);
$cat_from_db = $this->dbo->loadObjectList();
$category_existing = False;
$new_cat_alias = JFilterOutput::stringURLSafe($cat_title);
foreach ($cat_from_db as $cdb) {
if ($cdb->alias == $new_cat_alias) {
$category_existing = True;
echo "category already existing: " . $new_cat_alias . "\n";
}
}
// ----------------
if (!$category_existing) {
$values = [
"id" => null,
"title" => $cat_title,
"path" => $new_cat_alias,
"access" => 1,
"extension" => "com_content",
"published" => 1,
"language" => "*",
"created_user_id" => 0,
"params" => array (
"category_layout" => "",
"image" => "",
),
"metadata" => array (
"author" => "",
"robots" => "",
),
];
$tbl_cat->setLocation(1, "last-child");
if (!$tbl_cat->bind($values)) {
JError::raiseWarning( 500, $row->getError() );
return FALSE;
}
if (!$tbl_cat->check()) {
JError::raiseNotice(500, $article->getError());
return FALSE;
}
if (!$tbl_cat->store(TRUE)) {
JError::raiseNotice(500, $article->getError());
return FALSE;
}
$tbl_cat->rebuildPath($tbl_cat->id);
echo "category inserted: " . $tbl_cat->id . " - " . $new_cat_alias . "\n";
}
}
}
try {
JApplicationCli::getInstance('InsertCatExampleCli')->execute();
}
catch (Exception $e) {
// An exception has been caught, just echo the message.
fwrite(STDOUT, $e->getMessage() . "\n");
exit($e->getCode());
}
?>
Build | 3.4.1 | ⇒ | staging |
Title |
|
Title |
|
Title |
|
Title |
|
No, there ist no application registered - is this required with JApplicationCli scripts?
Seems to help - thanks. Didn't find this in API description or examples.
For safe bets it's a requirement. Most of the other JFactory objects can
build fine regardless of app, but for CLI you're better off setting it.
And ya that's documentation we need.
On Sunday, May 24, 2015, Stoffels.IT notifications@github.com wrote:
No, there ist no application registered - is this required with
JApplicationCli scripts?
This comment was created with the J!Tracker Application
https://github.com/joomla/jissues at issues.joomla.org/joomla-cms/7028
http://issues.joomla.org/tracker/joomla-cms/7028.—
Reply to this email directly or view it on GitHub
#7028 (comment).
Okay, I'll publish the working version as an example on github. Thanks for the fast reply. Is there a documentation site, where I can contribute?
Working version here:
https://github.com/stoffels-it/joomla-extensions-examples/blob/master/insert-cat-example.php
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2015-05-25 17:31:05 |
Closed_By | ⇒ | sarah-github |
I hit this same problem -- what do I need to add in order to register the app class ?
woops, please ignore my last question ... just spotted your 'working version' and that has all I need ... many thanks!
Hi There,
I was trying to use the code from the above working version to simply instantiate a JApplicationCli object, but it seems that my code just dies when the parent constructor is called. T1 is printed out on the terminal, but T2 is not. What am I doing wrong? Thanks!
<?php
// We are a valid entry point.
const _JEXEC = 1;
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 1);
// Load system defines
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';
// Load the configuration
require_once JPATH_CONFIGURATION . '/configuration.php';
// ----------------------
class TestCli extends JApplicationCli {
public function __construct() {
echo "T1\n";
parent::__construct();
echo "T2\n";
$this->config = new JConfig();
// important for storing several rows
JFactory::$application = $this;
}
public function doExecute() {
echo "T3\n";
}
}
echo "started...\n";
try {
JApplicationCli::getInstance('TestCli')->execute();
}
catch (Exception $e) {
// An exception has been caught, just echo the message.
fwrite(STDOUT, $e->getMessage() . "\n");
exit($e->getCode());
}
echo "finished...\n";
?>
@wanderlusted18 can you please open a new Issue as comments on closed Issues didn't get much Notice, thanks.
Did you register your app class to JFactory::$application? If not, you're
probably hitting the error because something is calling
JFactory::getApplication() and trying to boot a web app.
On Sunday, May 24, 2015, Stoffels.IT notifications@github.com wrote: