How to add a foreign key during migrations? Used by DoctrineMigrationsBundle

There is a house table:

enter image description here

And the user table:

enter image description here

+ migration code

public function up(Schema $schema) { $houseTable = $schema->getTable('house'); $table = $schema->createTable('user'); $table->addColumn('id', 'integer', ['autoincrement' => true, 'unsigned' => true]); $table->setPrimaryKey(['id']); $table->addColumn('first_name', 'string', ['length' => 255]); $table->addColumn('last_name', 'string', ['length' => 255]); $table->addColumn('password', 'string', ['length' => 255]); $table->addColumn('slat', 'string', ['length' => 255]); $table->addColumn('email', 'string', ['length' => 255]); $table->addColumn('created', 'datetime'); $table->addColumn('updated', 'datetime'); } 

I need to organize one-to-many communication (the user has a lot of housov). How to do it?

  • Just in case, I ask: what tool is used to describe migrations? - Dmitriy Simushev
  • @DmitriySimushev DoctrineMigrationsBundle - gudfar 4:42 pm
  • Added to description - gudfar
  • I usually do a connection in Entiti then a migration diff and the migration itself is ready - Serge Esmanovich
  • @SergeEsmanovich is it possible to make connections in the generation of an entity? I mean, when you do it from the console - gudfar

1 answer 1

 $houseTable->addForeignKeyConstraint($table, array('user_id'), array('id'));