I want to write a function that run migrate command i write below code but it doesn’t work:
<?php
namespace app\controllers;
use Yii;
use yii\console\controllers;
class ModulesmigrationController extends \yii\web\Controller
{
public function actionRun()
{
$migration = new Controllers\MigrateController;
$migration->run('migrate', ['migrationPath' => '@app/myPath/']);
}
}
but i got this error:
PHP Warning - yii\base\ErrorException
Missing argument 1 for yii\base\Controller::__construct(), called in
C:\xampp\htdocs\... on line 12 and defined
public function actionMigrate()
{
// see http://www.yiiframework.com/forum/index.php/topic/60000-run-migrate-command-in-a-function-in-yii2/
// and
// http://www.yiiframework.com/wiki/667/yii-2-list-of-path-aliases-available-with-default-basic-and-advanced-app/
$migration = new Controllers\MigrateController("migrate",Yii::$app);
return $migration->run('migrate', ['migrationPath' => '@console/migrations/']);
}
But I got a 404
If I simply
return "ok"
I see the ok, so is running the migrations the problem.
public function actionMigrateUp()
{
//default console commands outputs to STDOUT so this needs to be declared for wep app
if (!defined('STDOUT')) {
define('STDOUT', fopen('/tmp/stdout', 'w'));
}
//migration command begin
$migration = new \yii\console\controllers\MigrateController('migrate', Yii::$app);
$migration->runAction('up', ['migrationPath' => '@console/migrations/', 'interactive' => false]);
//migration command end
/**
* open the STDOUT output file for reading
* @var $message collects the resulting messages of the migrate command to be displayed in a view
*/
$handle = fopen('/tmp/stdout', 'r');
$message = '';
while (($buffer = fgets($handle, 4096)) !== false) {
$message.=$buffer . "<br>";
}
fclose($handle);
return $this->render('index', ['message' => $message]);
}
These are migrations details, but stdout is really capturing something because flash message, after manually change the page to the index, give me this
Yii Migration Tool (based on Yii v2.0.4)
Total 3 new migrations to be applied:
m130524_201442_init
m140506_102106_rbac_init
m150519_095654_create_family_table
*** applying m130524_201442_init
*** applied m130524_201442_init (time: 0.006s)
*** applying m140506_102106_rbac_init
*** applied m140506_102106_rbac_init (time: 0.070s)
*** applying m150519_095654_create_family_table
*** applied m150519_095654_create_family_table (time: 0.013s)
Migrated up successfully.
Why redirecting of STDOUT works only partially ?
I repeat: migrations are working well, stdout redirect is working only partially, so I cannot do a redirect