2010-06-17 52 views
3

我試圖執行cli命令./doctrine generate-migrations-diff和版本文件已經在正確的文件夾中正確創建。Doctrine generate-migrations-diff and migrate

的消息是:產生的遷移-DIFF - 生成的類移民成功地從差異

然後我試圖執行另一個CLI命令./doctrine遷移,在我看來是一個消息說:遷移 - 遷移成功地版本#1,但是當我打開類時,任何修改都已完成。爲什麼?

這是VERSION1文件:

<?php 

class Version1 extends Doctrine_Migration_Base 
{ 
public function up() 
{ 
    $this->removeColumn('addresses', 'address_test'); 
} 

public function down() 
{ 
    $this->addColumn('addresses', 'address_test', 'string', '', array(
     'fixed' => '0', 
     'unsigned' => '', 
     'primary' => '', 
     'notnull' => '1', 
     'autoincrement' => '', 
     )); 
} 
} 

?> 

這是YAML。 我刪除了字段:address_test

Addresses: 
    connection: doctrine 
    tableName: addresses 
    columns: 
    address_id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: true 
    address: 
     type: string() 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    city: 
     type: string(150) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    code: 
     type: string(20) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    country_id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    base: 
     type: integer(1) 
     fixed: false 
     unsigned: false 
     primary: false 
     default: '0' 
     notnull: true 
     autoincrement: false 
    latitude: 
     type: string(20) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    longitude: 
     type: string(20) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    customer_id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 

這是由遷移命令生成的BaseAddresses類: bindComponent( '地址', '教義');

/** 
* BaseAddresses 
* 
* This class has been auto-generated by the Doctrine ORM Framework 
* 
* @property integer $address_id 
* @property string $address 
* @property string $city 
* @property string $code 
* @property integer $country_id 
* @property integer $base 
* @property string $latitude 
* @property string $longitude 
* @property integer $customer_id 
* @property Countries $Countries 
* @property Customers $Customers 
* 
* @package ##PACKAGE## 
* @subpackage ##SUBPACKAGE## 
* @author  ##NAME## <##EMAIL##> 
* @version SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $ 
*/ 
abstract class BaseAddresses extends Doctrine_Record 
{ 
    public function setTableDefinition() 
    { 
     $this->setTableName('addresses'); 
     $this->hasColumn('address_id', 'integer', 4, array(
      'type' => 'integer', 
      'fixed' => 0, 
      'unsigned' => false, 
      'primary' => true, 
      'autoincrement' => true, 
      'length' => '4', 
      )); 
     $this->hasColumn('address', 'string', null, array(
      'type' => 'string', 
      'fixed' => 0, 
      'unsigned' => false, 
      'primary' => false, 
      'notnull' => false, 
      'autoincrement' => false, 
      'length' => '', 
      )); 
     $this->hasColumn('city', 'string', 150, array(
      'type' => 'string', 
      'fixed' => 0, 
      'unsigned' => false, 
      'primary' => false, 
      'notnull' => false, 
      'autoincrement' => false, 
      'length' => '150', 
      )); 
     $this->hasColumn('code', 'string', 20, array(
      'type' => 'string', 
      'fixed' => 0, 
      'unsigned' => false, 
      'primary' => false, 
      'notnull' => false, 
      'autoincrement' => false, 
      'length' => '20', 
      )); 
     $this->hasColumn('country_id', 'integer', 4, array(
      'type' => 'integer', 
      'fixed' => 0, 
      'unsigned' => false, 
      'primary' => false, 
      'notnull' => false, 
      'autoincrement' => false, 
      'length' => '4', 
      )); 
     $this->hasColumn('base', 'integer', 1, array(
      'type' => 'integer', 
      'fixed' => 0, 
      'unsigned' => false, 
      'primary' => false, 
      'default' => '0', 
      'notnull' => true, 
      'autoincrement' => false, 
      'length' => '1', 
      )); 
     $this->hasColumn('latitude', 'string', 20, array(
      'type' => 'string', 
      'fixed' => 0, 
      'unsigned' => false, 
      'primary' => false, 
      'notnull' => false, 
      'autoincrement' => false, 
      'length' => '20', 
      )); 
     $this->hasColumn('longitude', 'string', 20, array(
      'type' => 'string', 
      'fixed' => 0, 
      'unsigned' => false, 
      'primary' => false, 
      'notnull' => false, 
      'autoincrement' => false, 
      'length' => '20', 
      )); 
     $this->hasColumn('customer_id', 'integer', 4, array(
      'type' => 'integer', 
      'fixed' => 0, 
      'unsigned' => false, 
      'primary' => false, 
      'notnull' => true, 
      'autoincrement' => false, 
      'length' => '4', 
      )); 
    } 

    public function setUp() 
    { 
     parent::setUp(); 
     $this->hasOne('Countries', array(
      'local' => 'country_id', 
      'foreign' => 'country_id')); 

     $this->hasOne('Customers', array(
      'local' => 'customer_id', 
      'foreign' => 'customer_id', 
      'onDelete' => 'CASCADE')); 
    } 
} 

在一些網站,我已閱讀,我不得不執行命令:集結所有以產生更新地址類的版本,但我得到這個錯誤:

SQLSTATE[HY000]: General error: 1005 Can't create table 'web63db1.#sql-3e6_11d' (errno: 121). Failing Query: "ALTER TABLE addresses ADD CONSTRAINT addresses_customer_id_customers_customer_id FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE CASCADE". Failing Query: ALTER TABLE addresses ADD CONSTRAINT addresses_customer_id_customers_customer_id FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE CASCADE 

我做了什麼?提前致謝。

回答

6

遷移僅用於更改您的數據庫,而不是您的模型類。之後你需要建立你的模型類。

所以你正常的工作方式將是:

  1. 更改您的schema.yml
  2. 創建遷移文件(生成的遷移-DIFF)
  3. 運行你對數據庫遷移(遷移) ,你的數據庫現在應該改變
  4. 更新你的模型類(建設 - 所有)
+0

嗨Timo,遷移命令後我執行了構建 - 所有,但我得到這個錯誤:SQLSTATE [HY000]:一般錯誤:1007無法創建數據庫'mydb';數據庫存在。 – Michelangelo 2012-02-23 17:17:04

1

正如我不能發表評論,令m Ë修改蒂莫的回答是:

  1. 改變你的schema.yml
  2. 創建遷移文件(學說:產生的遷移-DIFF)(注:這是比較反對.class文件模式)
  3. 運行您的遷移(doctrine:migrate)(這將根據數據庫中的遷移版本號運行遷移)
  4. 構建您的類文件。 (學說:構建 - 所有類學說:構建模型&學說:構建表格&教條:構建過濾器)(注意:這將構建所有的類文件,但不會嘗試更新您的數據庫。)

我強烈建議在第一次運行任何遷移之前備份數據庫。如果遷移失敗(即因爲您輸入了模式),則最終可能會有一個數據庫停留在遷移之間,並且需要報廢。我通常會備份我的測試數據庫,然後導入活動數據庫以測試遷移。對於在現場服務器上遷移失敗,我感到非常害怕。

重要的是要注意'doctrine:generate-migrations-diff'比較schema.yml和你當前生成的類文件。 在生成遷移之前不要構建您的類

要在服務器上進行部署,您需要上傳schema.yml和新的遷移文件。然後,只需執行步驟3 & 4(然後根據習慣清除緩存),那麼您就很好。