Issues with console command.

I’m struggling with creating a simple console command. I created a new command class under protected/commands called sendEmail.php

sendEmail.php looks as follows


class sendEmailCommand extends CConsoleCommand{

    public function run($args){

        $arg1 = $args[0];

        $arg2 = $args[1];

        echo "Thanks for $arg1 and $arg2";

    }

    

    public function getHelp(){ 

        return 'Usage: <toNumber> <Message>'; 

    }

}

Now in /protected/config/console.php I have the following





return array(

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

	'name'=>'Email Application',

        

        // preloading 'log' component

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


	// autoloading model and component classes

	'import'=>array(

		'application.models.*',

		'application.components.*',

	),

        

    

	// application components

	'components'=>array(

		'user'=>array(

			// enable cookie-based authentication

			'allowAutoLogin'=>true,

		),

		'db'=>array(

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

                        'username'=>'asd',

                        'password'=>'sadasdas',

			'emulatePrepare'=>true,

		),

                'authManager'=>array(

                        'class'=>'CPhpAuthManager',

                ),

		'errorHandler'=>array(

			// use 'site/error' action to display errors

                    'errorAction'=>'site/error',

                ),

	        

               

	),

        'commandMap'=>array(

            'sendemail'=>array(

                'class'=>'sendEmail',

            ),

        

        ),


);

If I run the following command from the CLI within the protected folder




./yiic sendemail

I get the following error


./yiic sendemail e we

exception 'CException' with message 'sendEmail does not have a method named "run".' in .../base/CComponent.php:237

Stack trace:

I really do not know what could be wrong any advise will be appreciated.

I guess you have to rename the class to sendEmail (instead of sendEmailCommand).

What I though but in the documentation the Command needs to be included for any console commands?

Ok I thought this may not be the case if you use commandMap. Well rename the file to sendMailCommand.php and modify config accordingly. That’s the correct convention anyway.

Thanks, this worked :slight_smile: After changing it to sendMailCommand.php it worked, thanx for the tip. SOLVED

www.techportal.co.za