【问题标题】:Not able to create Table through YIIC Migreate create无法通过 YII Migrate 创建表
【发布时间】:2014-06-27 07:58:42
【问题描述】:

我刚开始学习 YII 框架,现在在尝试通过 YIIC MIGRATE 创建表时遇到问题。下面是我的表的代码。

class m140627_072906_create_test_table extends CDbMigration
{
   public function up()
   {
        $this->create('tbl_locations',
            array(
                'id'=>'pk',
                'location_name'=>'string NOT NULL',
                'create_time'=>'datetime NOT NULL',
                'create_user_id'=>'int(11) NOT NULL',
                'update_time'=>'datetime NOT NULL',
                'update_user_id'=>'int(11) NOT NULL',
            ), 'ENGINE=InnoDB'
        );
}
}
public function down()
{
    echo "m140627_072906_create_test_table does not support migration down.\n";
    return false;
}
}

我的项目在 C:\wamp\www\bookmyroom\ 下面是文件夹结构

C:\wamp\www\bookmyroom>目录 驱动器 C 中的卷没有标签。 卷序列号为 5291-FE18 C:\wamp\www\bookmyroom 目录

06/25/2014  08:58 PM    <DIR>          .
06/25/2014  08:58 PM    <DIR>          ..    
06/27/2014  12:32 PM    <DIR>          assets
06/25/2014  07:05 PM    <DIR>          css
06/25/2014  09:04 PM    <DIR>          framework
06/25/2014  07:05 PM    <DIR>          images
06/25/2014  07:05 PM               466 index-test.php
06/25/2014  09:10 PM               456 index.php
06/25/2014  07:08 PM    <DIR>          nbproject
06/25/2014  07:05 PM    <DIR>          protected
06/25/2014  07:05 PM    <DIR>          themes
               2 File(s)            922 bytes
               9 Dir(s)   8,986,624,000 bytes free

我在受保护的文件夹中运行了 YIIC MIgrate 命令。运行迁移命令时出现以下错误。

C:\wamp\www\bookmyroom\protected>YIIC Migrate

Yii Migration Tool v1.0 (based on Yii v1.1.14)

Total 1 new migration to be applied:
m140627_074906_create_table_locations

Apply the above migration? (yes|no) [no]:y
*** applying m140627_074906_create_table_locations
exception 'CException' with message 'm140627_074906_create_table_locations and its    
behaviors do not have a method or closure named "create".' in 
C:\wamp\www\yii\framework\base\CComponent.php:266
Stack trace:
#0         
C:\wamp\www\bookmyroom\protected\migrations\m140627_074906_create_table_locations.php
(16): CComponent->__call('create', Array)
#1             
C:\wamp\www\bookmyroom\protected\migrations\m140627_074906_create_table_locations.php
(16): m140627_074906_create_table_locations->create('tbl_locat
ions', Array, 'ENGINE=InnoDB')
#2 C:\wamp\www\yii\framework\cli\commands\MigrateCommand.php(385):            
m140627_074906_create_table_locations->up()
#3 C:\wamp\www\yii\framework\cli\commands\MigrateCommand.php(109): 
MigrateCommand->migrateUp('m140627_074906_...')
#4 [internal function]: MigrateCommand->actionUp(Array)
#5 C:\wamp\www\yii\framework\console\CConsoleCommand.php(172): 
ReflectionMethod->invokeArgs(Object(MigrateCommand), Array)
#6 C:\wamp\www\yii\framework\console\CConsoleCommandRunner.php(71): 
CConsoleCommand->run(Array)
#7 C:\wamp\www\yii\framework\console\CConsoleApplication.php(92): 
CConsoleCommandRunner->run(Array)
#8 C:\wamp\www\yii\framework\base\CApplication.php(180): 
CConsoleApplication->processRequest()
#9 C:\wamp\www\yii\framework\yiic.php(33): CApplication->run()
#10 C:\wamp\www\bookmyroom\protected\yiic.php(7): require_once('C:\wamp\www\yii...')
#11 {main}

您能否检查并告诉我我面临的错误。如果有任何整改要求,请告诉我。

【问题讨论】:

  • 大家好,我能够解决这个问题。我在代码中犯了一个错误。我只是在代码中使用了 create 而不是 createTable。谢谢大家。

标签: yii migrate yiic


【解决方案1】:

尝试使用执行

$this->execute("CREATE TABLE `tbl_locations` (
                'location_name'=>'string NOT NULL',
                'create_time'=>'datetime NOT NULL',
                'create_user_id'=>'int(11) NOT NULL',
                'update_time'=>'datetime NOT NULL',
                'update_user_id'=>'int(11) NOT NULL'
                PRIMARY KEY (`id`),
        ) ENGINE=InnoDB");

【讨论】:

  • 谢谢,问题是我使用了错误的代码 create() 而不是 createTable(),而且我会尝试在将来的迁移表中使用您的代码。
  • 此代码不起作用,它不是有效的 SQL。您仍然拥有数组中的 =&gt; 部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-31
  • 2013-10-19
  • 2020-06-01
  • 2018-06-22
  • 2021-10-17
  • 2019-01-13
  • 1970-01-01
相关资源
最近更新 更多