You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
How to run Yiic from your Yii application without shelling out to the console ¶
Introduction ¶
Sometimes it would be nice if we could run yiic from an action, for example a migrate database button in our admin panel. Or because you are on a host which doesn't have shell access. Or maybe you are not allowed run popen or exec.
Whatever reason, it turns out to be easy enough to add that feature to your application. :)
The following code runs 'yiic migrate --interactive=0' and echos the output.
Run yiic migrate in an action ¶
Code ¶
private function runMigrationTool() {
$commandPath = Yii::app()->getBasePath() . DIRECTORY_SEPARATOR . 'commands';
$runner = new CConsoleCommandRunner();
$runner->addCommands($commandPath);
$commandPath = Yii::getFrameworkPath() . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'commands';
$runner->addCommands($commandPath);
$args = array('command', 'yiic', 'migrate', '--interactive=0');
$command = $runner->createCommand($args[0]);
array_shift($args);
ob_start();
$runner->run($args);
echo htmlentities(ob_get_clean(), null, Yii::app()->charset);
}
How to use ¶
You can call it from a controller action like this:
actionMigrate() {
$this->runMigrationTool();
}
It's up to you to tailer it to your needs. Maybe let it return json or a string instead of echoing directly, so that you can hook it up with an ajax action to get realtime output.
Conclusion ¶
You can run any command you want, as long as it's available to yiic. It doesn't use console configuration at all.
This code is based on the webshell extension, so credit should go there.
Running multiple migrations via bash script
Thanks to your solution I've found a nice way to run multiple migrations. I had overlooked --interactive.
echo "Apply database migrations? (y/n)"; read choice if [ $choice == "y" ]; then echo "Applying migrations..."; ./yiic migrate --migrationPath=application.modules.p3admin.modules-install.user.migrations --migrationTable=tbl_migration_module_user --interactive=0 ./yiic migrate --migrationPath=application.modules.p3admin.modules-install.rights.migrations --migrationTable=tbl_migration_module_rights --interactive=0 ./yiic migrate --migrationPath=application.modules.p3widgets.migrations --migrationTable=tbl_migration_module_p3widgets --interactive=0 ./yiic migrate --migrationPath=application.modules.p3media.migrations --migrationTable=tbl_migration_module_p3media --interactive=0 else echo "Skipped." fi
Run in background
How to make the YIIC run in background? (the result output doesn't matter)
Or is it possible to run another simple shell command from Yiic?
@flyingangel: The command has to support it
Eg. yiic migrate does support a non-interactive mode, the webapp command not.
Does this work with globally configured command?
If yiic migrate is configured per these instructions (Configure Command Globally):
http://www.yiiframework.com/doc/guide/1.1/en/database.migration#customizing-migration-command
Will this method of running the migration still work using the configured options?
@PrplHaz4: Yes it will!
You can have a look at Phundament for an example, I've hooked yiic into composer script events with this method.
What about yii2
I'd like to suggest you to realize the same tutorial for Yii2, could be very usefull
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.