A while ago I made a couple of console commands extending from CConsoleCommand similar to what I had seen used in a CakePHP project - fixtures / fixturize - have a look in commands.zip
I had these commands working a while ago but seem to have forgotten how to execute them.
Have read through here again.
Below does not list the commands
cd [project-path]/protected
./yiic help
Below does not execute the command
cd [project-path]/protected
./yiic [command-name] [params]
Below produces an error - index.php does not exist or is not an entry script file.
To be honest, I don’t know if it’s normal. I just had the same problem, read about it in the forum and fixed it (I can’t remember the thread, that’s why I didn’t reference it).
Perhaps there is a way to configure yiic shell with the bootstrap, I’m affraid I don’t know. Perhaps a more senior member can tell us.
As it says on the help when you call yiic shell without stating a config file, it looks for a file called index.php. Since it’s not there yiic shell throws an error. So to make it work you need to state a config file.
Now that I think about it, having to state the config file seems reasonable at least in this case: imagine you have more than one application using the same base framework. Is more than likely that you’d like to run a command in the context of one of these applications. So there you go, you set this context when calling yiic shell.
If you were to have only one application using the base framework and can’t be bothered to write every time the config file path, then you can modify your framework/cli/commands/ShellCommand.php file and hard code the path.
gazbond, a word of advice. I’m pretty new to programming so most probably there will be a better way and safer practice. Let’s see if someone who knows better can tell us about it.
Sometimes you have to use the config file with your customized application. For a newly generated application it should work if you run the command from the application base directory
Hi there, I have a related issue trying to excecute a custom command. I think that I’m calling the commandMap’s class property in a wrong way. But a can’t figure this out.
With a fresh install of yii framework (latest release), I have made the following:
Put in /protected/commands/mycmd.php
Code of mycmd.php:
class MycmdCommand extends CConsoleCommand
{
public function run($args)
{
return "Im run() of Mycmd";
}
}
Code of /config/console.php:
return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'My Console Application',
// application components
'components'=>array(
'db'=>array(
'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
),
// uncomment the following to use a MySQL database
/*
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=testdrive',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
*/
),
'commandMap'=>array(
'mycmd'=>array(
'class'=>'Mycmd'
),
),
);
And executed like so:
./yiic mycmd
The error:
PHP Error[2]: include(Mycmd.php): failed to open stream: No such file or directory
in file /Developer/projects/yiibase/framework/YiiBase.php at line 396
#0 /Developer/projects/yiibase/framework/YiiBase.php(396): autoload()
#1 /Developer/projects/yiibase/framework/YiiBase.php(291): autoload()
#2 /Developer/projects/yiibase/framework/YiiBase.php(191): import()
#3 /Developer/projects/yiibase/framework/console/CConsoleCommandRunner.php(133): createComponent()
#4 /Developer/projects/yiibase/framework/console/CConsoleCommandRunner.php(60): CConsoleCommandRunner->createCommand()
#5 /Developer/projects/yiibase/framework/console/CConsoleApplication.php(88): CConsoleCommandRunner->run()
#6 /Developer/projects/yiibase/framework/base/CApplication.php(155): CConsoleApplication->processRequest()
#7 /Developer/projects/yiibase/framework/yiic.php(33): CConsoleApplication->run()
#8 /Developer/projects/yiibase/site/protected/yiic.php(7): require_once()
#9 /Developer/projects/yiibase/site/protected/yiic(4): require_once()