2013-07-03 158 views
1

我是新来的Yii(仍在学习)我下面一本书的教程 我在这里做了,因为它是写在书中创造了一个新的迁移Yii的迁移,表不创建

yiic migrate create create_issue_user_and_assignment_tables 

和safeup我写此查询

$this->createTable('tbl_issue', array(
'id' => 'pk', 
'name' => 'string NOT NULL', 
'description' => 'text', 
'project_id' => 'int(11) DEFAULT NULL', 
'type_id' => 'int(11) DEFAULT NULL', 
'status_id' => 'int(11) DEFAULT NULL', 
'owner_id' => 'int(11) DEFAULT NULL', 
'requester_id' => 'int(11) DEFAULT NULL', 
'create_time' => 'datetime DEFAULT NULL', 
'create_user_id' => 'int(11) DEFAULT NULL', 
'update_time' => 'datetime DEFAULT NULL', 
'update_user_id' => 'int(11) DEFAULT NULL', 
), 'ENGINE=InnoDB'); 
//create the user table 
$this->createTable('tbl_user', array(
'id' => 'pk', 
'username' => 'string NOT NULL', 
'email' => 'string NOT NULL', 
'password' => 'string NOT NULL', 
'last_login_time' => 'datetime DEFAULT NULL', 
'create_time' => 'datetime DEFAULT NULL', 
'create_user_id' => 'int(11) DEFAULT NULL', 
'update_time' => 'datetime DEFAULT NULL', 
'update_user_id' => 'int(11) DEFAULT NULL', 
), 'ENGINE=InnoDB'); 

而这safeDown()

$this->dropTable('tbl_issue'); 
$this->dropTable('tbl_user'); 

然后运行它,并得到了如下荷兰国际集团味精

D:\wamp\www\yiisite\protected>yiic migrate 
PHP Deprecated: Directive 'register_globals' is deprecated in PHP 5.3 and great 
er in Unknown on line 0 

Deprecated: Directive 'register_globals' is deprecated in PHP 5.3 and greater in 
Unknown on line 0 

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

Total 1 new migration to be applied: 
    m130703_085302_create_issue_user_and_assignment_tables 

Apply the above migration? (yes|no) [no]:yes 
*** applying m130703_085302_create_issue_user_and_assignment_tables 
*** applied m130703_085302_create_issue_user_and_assignment_tables (time: 0.042s 
) 


Migrated up successfully. 

现在的问题是,表不是在数据库中可能是因为味精中创造了register_globals的已被弃用,但我不知道该怎么办,连接参数是否正确,并插入一条记录在表中tbl_migration

m130703_085302_create_issue_user_and_assignment_ta... 1372842220 

但是没有创建新表。

回答

0

我只是干你跑了你的代码在它创建的表好吧,所以我不得不认为这是你的电话有什么问题。只有立即坚持的是你运行一个bash脚本而不是bat脚本。

yiic是一个bash脚本。在Windows上,建议运行yiic.batyiic.php

你能从命令行运行php吗?如果是这样的:

  • 从tbl_migrations
  • 运行删除迁移的迁移为 “C:\路径\到\ php.exe的yiic.php迁移了”
3

创建表通常穿上”牛逼需要交易

<?php 
class m130630_124600_some_description_name extends CDbMigration 
{ 
    public function up(){ 
     //upcode example create the session table 
     $this->createTable('session',[ 
      'id' => "varchar(40) NOT NULL", 
      'expire' => "int(12)", 
      'data' => "blob", 
     ]); 
     $this->addPrimaryKey('idx','session','id'); 
    } 
    public function down(){ 
     // downcode (undo the up code) example: drop session table 
     $this->dropTable('session'); 
    } 
} 

交易是否需要

如下safeUp的评论:

此方法包含应用此 迁移时将被执行的逻辑。此方法不同于up()之处在于此处实现的DB逻辑 将包含在数据库事务中。如果DB逻辑 需要位于事务中,则子类 类可以实现此方法而不是向上()

1

检查config/console.php文件 更改dbname,用户名和密码。 这是yiic命令使用的文件,而不是config/main。php 它现在应该工作

0

请确保您尚未通过MySQL手动创建tbl_usertbl_issue表。如果它们已经存在,请删除它们,然后再次运行迁移。

此外,请检查以确保您的数据库连接字符串main/config.phpmain/console.php设置为正确引用迁移应使用的数据库。

要确认它的工作原理,当您的迁移实际成功时,您将获得与以下内容非常相似的结果,列出所采取的每个SQL操作。如果在列表中看不到任何操作,则无法正常运行。

D:\xampp\htdocs\yii\trackstar\protected>yiic migrate 

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

Total 1 new migration to be applied: 
    m140406_014347_create_user_issue_assignment_tables 

Apply the above migration? (yes|no) [no]:y 
*** applying m140406_014347_create_user_issue_assignment_tables 
    > create table tbl_issue ... done (time: 0.351s) 
    > create table tbl_user ... done (time: 0.405s) 
    > create table tbl_project_user_assignment ... done (time: 0.366s) 
    > add foreign key fk_issue_project: tbl_issue (project_id) references tbl_pr 
oject (id) ... done (time: 0.923s) 
    > add foreign key fk_issue_owner: tbl_issue (owner_id) references tbl_user (
id) ... done (time: 1.066s) 
    > add foreign key fk_issue_requester: tbl_issue (requester_id) references tb 
l_user (id) ... done (time: 1.829s) 
    > add foreign key fk_project_user: tbl_project_user_assignment (project_id) 
references tbl_project (id) ... done (time: 1.416s) 
    > add foreign key fk_user_project: tbl_project_user_assignment (user_id) ref 
erences tbl_user (id) ... done (time: 1.032s) 
*** applied m140406_014347_create_user_issue_assignment_tables (time: 7.446s) 


Migrated up successfully. 

最后,在MySQL表的创建将隐式提交他们运行后,使运行在safeDown()safeUp()方法,这将运行代码,例如MySQL的交易代码,是不是特别有用。为了简单起见,我会将每种安全方法中的代码移至相应的“非安全”方法。

0

也许你在做错误,我做了什么。 yiic是一个控制台命令。在你的项目的config文件夹中检查你的db属性的console.php。

它可能指向testdrive.db SQLite默认一个&该表创建它们。

至少这是我的问题。

问候, Ajeet

1

也许你正在做的错误,我做了什么。 yiic是一个控制台命令。在你的项目的config文件夹中检查你的db属性的console.php。检查你的配置控制台。

它可能指向testdrive.db SQLite默认一个&该表创建它们。

至少这是我的问题。

此致,Ajeet

0

我也有同样的问题。这得到了由代码移动到

公共函数上() { 代码在这里 }

和由如下指令执行迁移解决

壳>的yiic向上迁移