migrations/Version20240502104621.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. /**
  7.  * Auto-generated Migration: Please modify to your needs!
  8.  */
  9. final class Version20240502104621 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'Create joined table with only two columns: person_id and point_of_interest_id. Add the foreign keys to the person and point_of_interest tables.';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // this up() migration is auto-generated, please modify it to your needs
  18.         $this->addSql('CREATE TABLE person_point_of_interests (person_id INT NOT NULL, point_of_interest_id INT NOT NULL, PRIMARY KEY(person_id, point_of_interest_id))');
  19.         $this->addSql('CREATE INDEX IDX_5A3D3C3A9E6F5DFB ON person_point_of_interests (person_id)');
  20.         $this->addSql('CREATE INDEX IDX_5A3D3C3A3B5E7B7C ON person_point_of_interests (point_of_interest_id)');
  21.         $this->addSql('ALTER TABLE person_point_of_interests ADD CONSTRAINT FK_5A3D3C3A9E6F5DFB FOREIGN KEY (person_id) REFERENCES person (id)');
  22.         $this->addSql('ALTER TABLE person_point_of_interests ADD CONSTRAINT FK_5A3D3C3A3B5E7B7C FOREIGN KEY (point_of_interest_id) REFERENCES point_of_interest (id)');
  23.         // Add all the values where referent_id in person table is not null
  24.         $this->addSql('INSERT INTO person_point_of_interests (person_id, point_of_interest_id) SELECT referent_id, id FROM point_of_interest WHERE referent_id IS NOT NULL');
  25.     }
  26.     public function down(Schema $schema): void
  27.     {
  28.         // this down() migration is auto-generated, please modify it to your needs
  29.         $this->addSql('DROP TABLE person_point_of_interests');
  30.     }
  31. }