Doctrine生成迁移差异和迁移

4
我尝试执行CLI命令./doctrine generate-migrations-diff,一个版本文件已经在正确的文件夹中成功创建。消息是:generate-migrations-diff - 从差异成功生成迁移类。然后我尝试执行另一个CLI命令./doctrine migrate,出现一条消息:migrate - 成功迁移到版本#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

这是由 migrate 命令生成的 BaseAddresses 类: bindComponent('Addresses', 'doctrine');
/**
 * 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'));
    }
}

在一些网站上我读到过,为了生成更新版本的类地址,我需要执行build-all命令,但是我收到了以下错误信息:

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

我该怎么做?提前谢谢。

2个回答

8

迁移仅用于更改数据库,而不是模型类。您需要在此之后构建模型类。

因此,您正常的工作方式将是:

  1. 更改您的 schema.yml
  2. 创建您的迁移文件(generate-migrations-diff)
  3. 运行迁移以针对数据库进行更改(migrate),您的数据库现在应该已更改
  4. 更新您的模型类(build-all)

嗨Timo,我执行了迁移命令后,执行了build-all,但是我遇到了这个错误:SQLSTATE [HY000]:General error: 1007 Can't create database 'mydb'; database exists. - Michelangelo

1

由于我无法评论,让我修改Timo的答案:

  1. 更改您的schema.yml
  2. 创建迁移文件(doctrine:generate-migrations-diff)(注意:这是将模式与类文件进行比较)
  3. 运行您的迁移(doctrine:migrate)(这将根据数据库中的迁移版本号运行迁移)
  4. 构建您的类文件。(doctrine:build --all-classes OR doctrine:build-model & doctrine:build-forms & doctrine:build-filters)(注意:这将构建所有类文件,但不会尝试更新您的数据库。)

我强烈建议在第一次运行任何迁移之前备份您的数据库。如果迁移失败(例如因为您在模式中打错了字),您可能会得到一个处于迁移之间的数据库,需要被废弃。我通常会备份我的测试数据库,然后导入实际数据库以测试迁移。我非常害怕在实际服务器上失败迁移。

重要的是要注意,“doctrine:generate-migrations-diff”将schema.yml与当前生成的类文件进行比较。在生成迁移之前不要构建您的类

要在服务器上部署,您需要上传您的schema.yml和新的迁移文件。然后只需执行第3步和第4步(并习惯性地清除缓存),就可以开始使用了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接