【发布时间】:2015-04-30 19:11:38
【问题描述】:
我创建了几个实体:
用户
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 symfony doctrine-orm doctrine migration