Can't access Yii::app()->params from within a CConsoleCommand?

I have a cron that triggers a yii command.

I created a cron bootstrap file that loads it’s own configuration file, for some reason the params array is empty. Is this expected? Is there something I need to call in my command to populate the params?

You probably use the wrong config file for your console app (!= config file for web app).

I use a different config for CRON jobs.

It seems like that would be the case but you can see for yourself that is not true.

Here is the code:

yiic-daemon.php


<?php


// change the following paths if necessary

$yiic=dirname(__FILE__).'/../yii-1.1.5.r2654/framework/yiic.php';

$config=dirname(__FILE__).'/config/cron.php';


require_once($yiic);

config/cron.php


<?php


// This is the configuration for yiic console application.

// Any writable CConsoleApplication properties can be configured here.

return array(

        'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

        'name'=>'Cron Daemon',


  // preloading 'log' component

  'preload'=>array('log'),


  'import' => array(

    'application.models.*',

    'application.components.*',

    'application.helpers.*',

    'ext.mail.Message',

  ),

  'components'=>array(

    'db'=>array(

    ),

    'mail'=>array(

    ),

    'log'=>array(

      'class'=>'CLogRouter',

      'routes'=>array(

        array(

          'class'=>'CFileLogRoute',

          'levels'=>'error, warning, info',

        ),

      ),

    ),

  ),


  // application-level parameters that can be accessed

  // using Yii::app()->params['paramName']

  'params'=>array(

    'testParam'=>10,

  ),

);

command to execute: yiic-daemon daemon

command/DaemonCommand.php


<?php

class DaemonCommand extends CConsoleCommand

{

  public function getHelp()

  {

    return 'Daemon command.';

  }


  /**

   *

   *

   * @param unknown_type $args

   */

  public function run($args)

  {

    echo 'test = ' . Yii::app()->params['testParam'];

  }

}

bump

I think I’m pretty sure you’ll need to instantiate a console application object




$app=Yii::createConsoleApplication($config);



or just access the config array directly.

/Tommy

@tri: Should not be necessary, the default yiic.php created with a webapp also works like this. Only $config must be set.

@DarkNFS: Your setup looks fine for me, you must be overlooking something. It’s not a bug, as this feature is working fine for me (and others): I can access app parameters from console applications.

I stand corrected, was thinking of yii.php instead of yiic.php.

(yiic.php will call createConsoleApplication().)

/Tommy

i have the same problem.there will be a error:

exception ‘CException’ with message ‘Object configuration must be an array containing a “class” element.’ in /Library/WebServer/Documents/faceon/yii/framework/YiiBase.php:188

Stack trace:

#0 /Library/WebServer/Documents/faceon/yii/framework/base/CModule.php(371): YiiBase::createComponent(Array)

#1 /Library/WebServer/Documents/faceon/yii/framework/base/CModule.php(86): CModule->getComponent(‘params’)

#2 /Library/WebServer/Documents/faceon/faceon_server/trunk/protected/commands/UploadCommand.php(33): CModule->__get(‘params’)

#3 [internal function]: UploadCommand->actionUpload()

#4 /Library/WebServer/Documents/faceon/yii/framework/console/CConsoleCommand.php(135): ReflectionMethod->invokeArgs(Object(UploadCommand), Array)

#5 /Library/WebServer/Documents/faceon/yii/framework/console/CConsoleCommandRunner.php(63): CConsoleCommand->run(Array)

#6 /Library/WebServer/Documents/faceon/yii/framework/console/CConsoleApplication.php(88): CConsoleCommandRunner->run(Array)

#7 /Library/WebServer/Documents/faceon/yii/framework/base/CApplication.php(155): CConsoleApplication->processRequest()

#8 /Library/WebServer/Documents/faceon/yii/framework/yiic.php(33): CApplication->run()

#9 /Library/WebServer/Documents/faceon/faceon_server/trunk/protected/yiic.php(8): require_once(’/Library/WebSer…’)

#10 /Library/WebServer/Documents/faceon/faceon_server/trunk/protected/yiic(4): require_once(’/Library/WebSer…’)

#11 {main}

Seems like you placed the params array inside the components array.

/Tommy

thanks Tommy.I was too careless.

Thanks Tommy, from my side as well!! :smiley: … I was looking for the cause of this error for 3 hours now :-/ … anyway: thanks a lot! :wink: