php - 为什么 Doctrine 要创造一场大迁徙?

标签 php symfony doctrine-orm doctrine migration

我创建了几个实体:

用户

namespace App\Models;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=32, nullable=false)
     */
    protected $firstName;

    /**
     * @ORM\Column(type="string", length=32, nullable=false)
     */
    protected $lastName;

    /**
     * @ORM\Column(type="string", length=100, unique=true, nullable=false)
     */
    protected $userName;

    /**
     * @ORM\Column(type="string", length=100, unique=true, nullable=false)
     * @Assert\Email
     */
    protected $email;

    /**
     * @ORM\Column(type="string", length=500, nullable=false)
     */
    protected $password;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    protected $created_at;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    protected $updated_at;

    ...
}

图片

namespace App\Models;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="images")
 * @ORM\HasLifecycleCallbacks
 */
class Image {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="\App\Models\User")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user_id;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    protected $created_at;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    protected $updated_at;

    /**
     * @ORM\Column(type="string", nullable=false)
     */
    protected $location;

    /**
     * @ORM\Column(type="integer", nullable=false)
     */
    protected $imageSize;

    ....
}

当我运行'vendor\bin\doctrine'migrations:diff时,它会继续并生成迁移。不过只有一次迁移...

迁移

namespace ImageUplaoder\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
class Version20150430210959 extends AbstractMigration
{
    /**
     * @param Schema $schema
     */
    public function up(Schema $schema)
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

        $this->addSql('CREATE TABLE images (id INT AUTO_INCREMENT NOT NULL, user_id INT DEFAULT NULL, created_at DATETIME DEFAULT NULL, updated_at DATETIME DEFAULT NULL, location VARCHAR(255) NOT NULL, imageSize INT NOT NULL, INDEX IDX_E01FBE6AA76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB');
        $this->addSql('CREATE TABLE users (id INT AUTO_INCREMENT NOT NULL, firstName VARCHAR(32) NOT NULL, lastName VARCHAR(32) NOT NULL, userName VARCHAR(100) NOT NULL, email VARCHAR(100) NOT NULL, password VARCHAR(500) NOT NULL, created_at DATETIME DEFAULT NULL, updated_at DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_1483A5E9586CA949 (userName), UNIQUE INDEX UNIQ_1483A5E9E7927C74 (email), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB');
        $this->addSql('ALTER TABLE images ADD CONSTRAINT FK_E01FBE6AA76ED395 FOREIGN KEY (user_id) REFERENCES users (id)');
    }

    /**
     * @param Schema $schema
     */
    public function down(Schema $schema)
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

        $this->addSql('ALTER TABLE images DROP FOREIGN KEY FK_E01FBE6AA76ED395');
        $this->addSql('DROP TABLE images');
        $this->addSql('DROP TABLE users');
    }
}

这不是我想要的,我想要单独的迁移。我是否可以不一次创建多个实体,然后运行某种命令来生成彼此独立的迁移?

最佳答案

首先,迁移工具只是一个工具。它并不是一种从版本生成路径的万无一失的方法。一般情况下,它用作模板,您将手动修改它。

具体回答您的问题,因为该工具旨在比较整体架构。考虑到实体之间存在关系,无论如何都不可能评估单个实体。

不过,我不太明白您想要拆分迁移的原因。通常,这是一个要么全有要么全无的交易,因为您的应用程序需要一个单一版本(除非您的应用程序真的很聪明!)。

长话短说,它只是一个开发人员工具,而不是问题的解决方案。

关于php - 为什么 Doctrine 要创造一场大迁徙?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29975865/

相关文章:

Symfony 5 - 在类别 url 中显示 id 和 slug,并在 slug 更新时通过 id 找到它

arrays - 反射异常 : Class ArrayCollection does not exist

php - Doctrine 2 findBy* 返回对象

php - Symfony2/Doctrine2 (MySQL) 更新后未立即显示在选择上

java - 查找所有 "character-equal"字符串的高效算法?

php - LIMIT 和 OFFSET 可以帮助减少大表的执行时间

php - Symfony Router UrlMatcher 中的 match 参数和 RequestContext 之间的区别

php - 在本地托管 Laravel

javascript - html keygen替代方案,在浏览器中生成 key 对

php - Symfony2 记住我不适用于 fos 用户包