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.
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('yiic', 'migrate', '--interactive=0'); ob_start(); $runner->run($args); echo htmlentities(ob_get_clean(), null, Yii::app()->charset); }
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.
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.
Total 5 comments
You can have a look at Phundament for an example, I've hooked yiic into composer script events with this method.
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?
Eg. yiic migrate does support a non-interactive mode, the webapp command not.
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?
Thanks to your solution I've found a nice way to run multiple migrations. I had overlooked --interactive.
Leave a comment
Please login to leave your comment.