yiic CConsoleCommand and yii-environment

My new client is using yii framework, he’s very impressed … I’m still a yiinoob looking for answers though :huh:

I’m trying to create a CConsoleCommand to perform a db bootstrap, useful prior to DB Migrations if someone wants a clean start because their tests have crapped it up, new developers etc.

I’ve noticed that CConsoleCommand::init is not called as the api says. My child class overrides, but unless I invoke it myself in run($args) it is not called.

CConsoleCommandRunner calls init() right before it calls run() on the instantiated CConsoleCommand. I can’t imagine a reason why this shouldn’t work. Can you post an example of the code that’s not working?

if you override __construct method be sure to call parent implementation first

I have a simple BootstrapdbCommand extends CConsoleCommand in the command path.

./yiic shell

and help shows it in the list.

I put an echo in CConsoleCommandRunner announcing the invocation of the init method. There I discovered it’s calling ShellCommand::init() rather than my BootstrapdbCommand::init()

edit: in fact in the trackstar app I’m getting the same thing. I added echoes to all the various inits as in

echo get_class($this).": message text whatever\n";

ShellCommand lets the ancestor CConsoleCommand parent init() handle it.

kevin@ubuntuhagelbg:/var/www/trackstar/protected$ ./yiic shell ../index.php 

CConsoleCommandRunner: CALLING command's init method, command is a ShellCommand

ShellCommand: init method invoked

Yii Interactive Tool v1.1 (based on Yii v1.1.<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' />

Please type 'help' for help. Type 'exit' to quit.

>> rbac

RbacCommand: run() called


I see. You’re referring to the case of executing your command from within the yiic shell? I must admit, that I didn’t use that shell for quite some time… You’re aware of the fact that you don’t need to use that shell?

For example, my commands are stored under protected/commands (default for CConsoleApplication::commandPath) and I use them this way:

D:\wamp\www\yiiSignalsDemo\trunk>protected\yiic.bat help

Yii command runner (based on Yii v1.1.7)

Usage: D:\wamp\www\yiiSignalsDemo\trunk\protected\yiic.php <command-name> [parameters...]

The following commands are available:

 - message

 - migrate

 - shell

 - signal    <-- custom one

 - webapp

To see individual command help, use the following:

   D:\wamp\www\yiiSignalsDemo\trunk\protected\yiic.php help <command-name>

D:\wamp\www\yiiSignalsDemo\trunk>protected\yiic.bat help signal

Usage: D:\wamp\www\yiiSignalsDemo\trunk\protected\yiic.php signal <action>



    connect --signalId=value --cmd=value

D:\wamp\www\yiiSignalsDemo\trunk>protected\yiic.bat signal list


No need to enter the yiic shell first. Maybe this can help.

For the invocation of commands through yiic shell, I can confirm, that CustomCommand::init() won’t be called.

I’ve moved into that use now, leaving the shell. I was overriding teh constructor without calling the ancestor constructor with the args, and I was overriding CConsoleCommand::run($args) as described in the agile yii book. I had no good reason to do that in my bootstrapdb method. As soon as I removed my override the init() was called by the console runner

Glad it works for you now.

I kept digging for the reason of that bug and prepared a small patch.

Maybe it can help others or be applied to the framework (review shouldn’t be complicated ;)).

Could you maybe create a ticket for it at Google.code? :)

Issue 2636: console commands don’t get initialized by yiic shell