?
avatar sarah-github
sarah-github
25 May 2015

Steps to reproduce the issue

JTableCategory used in JApplicationCli like in ExampleApp in code below.

Expected result

Categories should be added.

Actual result

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.)

System information (as much as possible)

PHP 5.4.36-0+deb7u3
Joomla! 3.4.1

Additional comments

*************************/

// 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());
}

?>

avatar sarah-stoffels-it sarah-stoffels-it - open - 25 May 2015
avatar sarah-stoffels-it sarah-stoffels-it - change - 25 May 2015
Build 3.4.1 staging
avatar mbabker
mbabker - comment - 25 May 2015

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:

Steps to reproduce the issue

JTableCategory used in JApplicationCli like in ExampleApp in code below.
Expected result

Categories should be added.
Actual result

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.)
System information (as much as possible)

PHP 5.4.36-0+deb7u3
Joomla! 3.4.1
Additional comments

<?php
/************************
Example Client App for inserting categories

*************************/

// 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());
}

?>


Reply to this email directly or view it on GitHub
#7028.

avatar sarah-github sarah-github - change - 25 May 2015
Title
Application Instantiation Error when using JTable store for category in JApplicationCli
replaced
avatar sarah-stoffels-it sarah-stoffels-it - change - 25 May 2015
The description was changed
Title
Application Instantiation Error when using JTable store for category in JApplicationCli
replaced
avatar sarah-github sarah-github - change - 25 May 2015
Title
replaced
Application Instantiation Error when using JTable store for category in JApplicationCli
avatar sarah-stoffels-it sarah-stoffels-it - change - 25 May 2015
The description was changed
Title
replaced
Application Instantiation Error when using JTable store for category in JApplicationCli
avatar sarah-github
sarah-github - comment - 25 May 2015

No, there ist no application registered - is this required with JApplicationCli scripts?


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/7028.

avatar sarah-github
sarah-github - comment - 25 May 2015

Seems to help - thanks. Didn't find this in API description or examples.


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/7028.

avatar mbabker
mbabker - comment - 25 May 2015

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).

avatar sarah-github
sarah-github - comment - 25 May 2015

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?


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/7028.

avatar sarah-github sarah-github - close - 25 May 2015
avatar sarah-github sarah-github - change - 25 May 2015
Status New Closed
Closed_Date 0000-00-00 00:00:00 2015-05-25 17:31:05
Closed_By sarah-github
avatar sarah-github sarah-github - close - 25 May 2015
avatar rhellyer
rhellyer - comment - 18 Oct 2015

I hit this same problem -- what do I need to add in order to register the app class ?

avatar rhellyer
rhellyer - comment - 18 Oct 2015

woops, please ignore my last question ... just spotted your 'working version' and that has all I need ... many thanks!

avatar wanderlusted18
wanderlusted18 - comment - 14 Nov 2018

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";
?>
avatar franz-wohlkoenig
franz-wohlkoenig - comment - 14 Nov 2018

@wanderlusted18 can you please open a new Issue as comments on closed Issues didn't get much Notice, thanks.

avatar wanderlusted18
wanderlusted18 - comment - 14 Nov 2018

I created a new issue here:
#23075

Thx!

Add a Comment

Login with GitHub to post a comment