Execute Code During Yii Startup (Before First Action)


i have a webapplication which upgrades themself its database. (so users have only copy the files of the new application version and db upgrading is done automatically)

i have extended the MigrateCommand-class to use the integrated yii migration feature. this works fine.

my problem is:

i have to execute this db-migration code. (first check if db upgrade is required and ugrade if necessary)

it would be great to execute it directly before the first action is called. this means i already need all yii functions for db-migration, but it should be executed BEFORE the action.

is there any documention about the start process of YII? maybe i can extend some class and inject my db-upgrade code

We added a filter to our base controller (protected/components/Controller.php)

    public function filters()


        return array('checkDatabase','accessControl');


    public function filterCheckDatabase($filterChain)





And then checkDatabase looks like this:

    public static function checkDatabase()


        $run_migration = false;

        $very_first_run = false;

        if (defined('RUNNING_TESTS'))


        // check database


        // if we can't get connection then fail

        if (!$connection)


        // count loaded migrations

        $sql = "select count(*) from tbl_migration";


        try {

            $db_migs = @$command->queryScalar();

        } catch (CDbException $e) {

            // if we fail the query do migration

            $very_first_run = true;


        if ($db_migs === false)

            $run_migration = true;



            // don't count "base" migration


            // count file migrations

            $pattern = dirname(__FILE__).'/../migrations/m*.php';

            $files = glob($pattern);

            $run_migration = count($files) > $db_migs;


        if ($run_migration)



            Alerts::log("Database structure updated.", Alerts::SEVERITY_INFO);

            if (! $very_first_run)

                Yii::app()->user->setFlash('success', "Updated database structure.");




    public static function runMigration($down = false)


        $runner = new CConsoleCommandRunner();

        $commandPath = Yii::getFrameworkPath() . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'commands';


        $args = array('yiic', 'migrate', '--interactive=0');

        if ($down) 

            $args[] = 'down'; 



        return ob_get_clean();


Check the Guide for info on the application life cycle. Implement an onBeginRequest event to check if the upgrade is required and if so, act on it.

both ideas are great!!