Yii框架:迁移不会创建/删除表

时间:2013-09-19 10:01:44

标签: php database web yii database-migration

尝试使用Yii迁移删除一个表并创建另一个表。 这是代码:

    <?php

class m130919_095159_create_offer_tables extends CDbMigration
{
    public function up()
    {
        $this->getDbConnection()->createCommand()->dropTable('offer');

        $this->createTable('settings', array(
            'id' => 'pk',
            'offer_id' => 'integer NOT NULL',
            'system_id' => 'integer NULL',  
            'site_title' => 'varchar(255) NULL',
            'index_number' => 'integer NULL',  
            'coupon_token' => 'varchar(255) NULL',
            'facebook_app_id' => 'varchar(255) NULL',
            'facebook_app_secret' => 'varchar(255) NULL',
        ));

        $this->createTable('content', array(
            'id' => 'pk',
            'offer_id' => 'integer NOT NULL',
            'created' => 'datetime NOT NULL',
            'modified' => 'datetime NOT NULL',
            'status' => 'integer NOT NULL',
            'title_uz' => 'varchar(255) NULL',
            'title_ru' => 'varchar(255) NULL',
            'description_uz' => 'text NULL',
            'description_ru' => 'text NULL', 
        ));
    }

    public function down()
    {
        echo "m130919_095159_create_offer_tables does not support migration down.\n";
        return false;
    }
}

执行命令php yiic.php migrate后得到答案migrated up successfully。问题是sql命令尚未执行。表没有丢弃,另一个表没有创建。数据库没有变化。找不出原因

2 个答案:

答案 0 :(得分:0)

你正在以错误的方式使用迁移。正确的方法是你在up函数中做的任何事情只是尝试在down函数中做相反的事情。

假设你在这里丢弃一个表并在up函数中创建两个表,那么你应该在down函数中编写代码

创建表格的表格代码,用于放入和删除用于创建的两个表格的表格代码

首先检查console.php中的数据库配置

public function up() {
  $this->dropTable('offer');
  $this->createTable('settings', array(
      'id' => 'pk',
      'offer_id' => 'integer NOT NULL',
      'system_id' => 'integer NULL',  
      'site_title' => 'varchar(255) NULL',
      'index_number' => 'integer NULL',  
      'coupon_token' => 'varchar(255) NULL',
      'facebook_app_id' => 'varchar(255) NULL',
      'facebook_app_secret' => 'varchar(255) NULL',
  ));
   $this->createTable('content', array(
      'id' => 'pk',
      'offer_id' => 'integer NOT NULL',
      'created' => 'datetime NOT NULL',
      'modified' => 'datetime NOT NULL',
      'status' => 'integer NOT NULL',
      'title_uz' => 'varchar(255) NULL',
      'title_ru' => 'varchar(255) NULL',
      'description_uz' => 'text NULL',
      'description_ru' => 'text NULL', 
  ));
}

public function down(){
  $this->createTable('offer', array(
    //attributes for offer table
  ));
  $this->dropTable('settings');
  $this->dropTable('content');
} 

答案 1 :(得分:0)

您必须在config目录下的main.php和console.php上正确配置数据库。如果您使用MySQL取消注释该部分并在sqlite下注释testdrive.db。