View Revisions: Issue #15500

Summary 15500: getSurveyInfo() fails with fatal error for a plugin from console when tokens table with attributes set
Revision 2019-10-31 05:01 by adamzammit
Description When using the function getSurveyInfo() inside a plugin that is executed from the console AND the survey I am getting the info for has a tokens table AND has attributes set in the tokens table, I received the error:

PHP Fatal error: Class 'Token_XXXXXX' not found in /var/www/html/framework/db/ar/CActiveRecord.php on line 403

If the same function was called in the plugin NOT from the console, there was no error. Also if there was no tokens table set or a tokens table set with no attributes - there was no error.

I was able to track this down to the console init not importing and registering the ClassFactory.

Here is my solution:

In application/commands/console.php add the 3 lines indicated with "//add"

    if (isset($config)) {
        require_once($core.'ConsoleApplication.php');
        $app = Yii::createApplication('ConsoleApplication', $config);
        define('APPPATH', Yii::app()->getBasePath().DIRECTORY_SEPARATOR);
        $app->commandRunner->addCommands(YII_PATH.'/cli/commands');
        $env = (string) @getenv('YII_CONSOLE_COMMANDS');
        if (!empty($env)) {
            $app->commandRunner->addCommands($env);
        }

        Yii::import('application.helpers.ClassFactory'); //add
        ClassFactory::registerClass('Token_', 'Token'); //add
       ClassFactory::registerClass('Response_', 'Response'); //add
     }
    $app->run();


Not sure if anyone else has encountered this or is happy to test?
Revision 2019-10-31 05:01 by adamzammit
Description When using the function getSurveyInfo() inside a plugin that is executed from the console AND the survey I am getting the info for has a tokens table AND has attributes set in the tokens table, I received the error:

PHP Fatal error: Class 'Token_XXXXXX' not found in /var/www/html/framework/db/ar/CActiveRecord.php on line 403

If the same function was called in the plugin NOT from the console, there was no error. Also if there was no tokens table set or a tokens table set with no attributes - there was no error.

I was able to track this down to the console init not importing and registering the ClassFactory.

Here is my solution:

In application/commands/console.php add the 3 lines indicated with "<- add"

    if (isset($config)) {
        require_once($core.'ConsoleApplication.php');
        $app = Yii::createApplication('ConsoleApplication', $config);
        define('APPPATH', Yii::app()->getBasePath().DIRECTORY_SEPARATOR);
        $app->commandRunner->addCommands(YII_PATH.'/cli/commands');
        $env = (string) @getenv('YII_CONSOLE_COMMANDS');
        if (!empty($env)) {
            $app->commandRunner->addCommands($env);
        }

        Yii::import('application.helpers.ClassFactory'); <- add
        ClassFactory::registerClass('Token_', 'Token'); <- add
       ClassFactory::registerClass('Response_', 'Response'); <- add
     }
    $app->run();


Not sure if anyone else has encountered this or is happy to test?
Revision 2019-10-31 05:00 by adamzammit
Description When using the function getSurveyInfo() inside a plugin that is executed from the console AND the survey I am getting the info for has a tokens table AND has attributes set in the tokens table, I received the error:

PHP Fatal error: Class 'Token_XXXXXX' not found in /var/www/html/framework/db/ar/CActiveRecord.php on line 403

If the same function was called in the plugin NOT from the console, there was no error. Also if there was no tokens table set or a tokens table set with no attributes - there was no error.

I was able to track this down to the console init not importing and registering the ClassFactory.

Here is my solution:

In application/commands/console.php add the 3 lines indicated with a "+"

    if (isset($config)) {
        require_once($core.'ConsoleApplication.php');
        $app = Yii::createApplication('ConsoleApplication', $config);
        define('APPPATH', Yii::app()->getBasePath().DIRECTORY_SEPARATOR);
        $app->commandRunner->addCommands(YII_PATH.'/cli/commands');
        $env = (string) @getenv('YII_CONSOLE_COMMANDS');
        if (!empty($env)) {
            $app->commandRunner->addCommands($env);
        }
+ Yii::import('application.helpers.ClassFactory');
+ ClassFactory::registerClass('Token_', 'Token');
+ ClassFactory::registerClass('Response_', 'Response');
    }
    $app->run();


Not sure if anyone else has encountered this or is happy to test?