Accessing Model in Console Application

Hi,

I’m running into this error when trying to access the model classes, through findAll(), in a console app.

My Config file looks like:




return array(

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

    'name' => 'Project Browser Console Application',

    // application components

    'import' => array(

        'application.models.*',

        'application.library.*',

        'application.library.components.*',

    ),

    'components' => array(

        'db' => array(

            'connectionString' => 'mysql:host=localhost;dbname=prog08',

            'emulatePrepare' => true,

            'username' => 'root',

            'password' => '',

            'charset' => 'utf8',

            'tablePrefix' => 'tbl_',

        ),

    ),

);



Any ideas?

Matt

By default the console config doesn’t inherit settings from the main.php config file. to get a component enabled, you have to specify it in the console config explicitly, this goes for the user component which is the error you have found.

You can also merge configs.

  • config/base.php: contains configuration shared by web app and console app (maybe like your DB connection?)

  • config/console.php: contains configuration which is specific to the console app (maye the name?)

  • config/main.php: contains configuration which is specific to the web app (like default controller - console apps don’t have such a property)

Then, you write config/base.php as you’re used to write it, simply return the array:




// This is the application's base configuration. Any writable

// CWApplication properties can be configured here.

return array(

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

  'name'=>'Signals Demo',


  // preloading 'log' component

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


  [...]

};



For the main config and the console config, you merge the base config with the stuff that need to be specialized:




return CMap::mergeArray( require(dirname(__FILE__).'/base.php'), array(

  'name'=>'Signals Demo Console',

  'components' => array(

    'c1' => array(

      [...]

    ),

  ),

  [..]

));



This way, you don’t have to configure everything twice.

Thanks, makes sense.

I’ve added the user component, which point to WebUser (which extends CWebUser). This is calling the init function of WebUser.




        public function init()

	{

		parent::init();

		Yii::app()->getSession()->open();

		if($this->getIsGuest() && $this->allowAutoLogin)

			$this->restoreFromCookie();

		else if($this->autoRenewCookie && $this->allowAutoLogin)

			$this->renewCookie();

		if($this->autoUpdateFlash)

			$this->updateFlash();


		$this->updateAuthStatus();

	}



Now, I’m getting error “CConsoleApplication doesn’t have a method named ‘getSession’.”

If I comment out the the body of the method, all’s good. Is the CConsoleApp supposed to load the WebUser component or am I supposed to use a Console specific component?

Thanks,

Matt

How can I use actions in which I use userGroups in Commands application?

Here the error if I simply try to use them in my app:


exception 'CException' with message 'CConsoleApplication and its behaviors do not have a method or closure named "getSession".' in /var/www/mandolini/www/yii/framework/base/CComponent.php:269

How can I log in as administrator in console mode?

you can’t

session is a component of a web application

console commands are supposed to be executed internally

take a look here also here

thanks