2013-07-03 65 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向上遷移