createJunctionMigration.php 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * This view is used by console/controllers/MigrateController.php
  4. * The following variables are available in this view:
  5. * @since 2.0.7
  6. * @deprecated since 2.0.8
  7. */
  8. /* @var $className string the new migration class name without namespace */
  9. /* @var $namespace string the new migration class namespace */
  10. /* @var $table string the name table */
  11. /* @var $field_first string the name field first */
  12. /* @var $field_second string the name field second */
  13. echo "<?php\n";
  14. if (!empty($namespace)) {
  15. echo "\nnamespace {$namespace};\n";
  16. }
  17. ?>
  18. use yii\db\Migration;
  19. /**
  20. * Handles the creation of table `<?= $table ?>` which is a junction between
  21. * table `<?= $field_first ?>` and table `<?= $field_second ?>`.
  22. */
  23. class <?= $className ?> extends Migration
  24. {
  25. /**
  26. * @inheritdoc
  27. */
  28. public function up()
  29. {
  30. $this->createTable('<?= $table ?>', [
  31. '<?= $field_first ?>_id' => $this->integer(),
  32. '<?= $field_second ?>_id' => $this->integer(),
  33. 'PRIMARY KEY(<?= $field_first ?>_id, <?= $field_second ?>_id)',
  34. ]);
  35. $this->createIndex(
  36. 'idx-<?= $table . '-' . $field_first ?>_id',
  37. '<?= $table ?>',
  38. '<?= $field_first ?>_id'
  39. );
  40. $this->createIndex(
  41. 'idx-<?= $table . '-' . $field_second ?>_id',
  42. '<?= $table ?>',
  43. '<?= $field_second ?>_id'
  44. );
  45. $this->addForeignKey(
  46. 'fk-<?= $table . '-' . $field_first ?>_id',
  47. '<?= $table ?>',
  48. '<?= $field_first ?>_id',
  49. '<?= $field_first ?>',
  50. 'id',
  51. 'CASCADE'
  52. );
  53. $this->addForeignKey(
  54. 'fk-<?= $table . '-' . $field_second ?>_id',
  55. '<?= $table ?>',
  56. '<?= $field_second ?>_id',
  57. '<?= $field_second ?>',
  58. 'id',
  59. 'CASCADE'
  60. );
  61. }
  62. /**
  63. * @inheritdoc
  64. */
  65. public function down()
  66. {
  67. $this->dropTable('<?= $table ?>');
  68. }
  69. }