migrations/Version20220413125627.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 Version20220413125627 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'TeamAka';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // this up() migration is auto-generated, please modify it to your needs
  18.         $this->connection->executeQuery('ALTER TABLE aka ADD team_id INT DEFAULT NULL');
  19.         $lastId 0;
  20.         while (true) {
  21.             $akas $this->connection->fetchAllAssociative("SELECT idaka, idelement_aka FROM aka WHERE typeelement_aka = 2 AND idaka > {$lastId} LIMIT 1000");
  22.             if (empty($akas)) {
  23.                 break;
  24.             }
  25.             foreach ($akas as $aka) {
  26.                 $akaId $lastId $aka['idaka'];
  27.                 $akaElementId $aka['idelement_aka'];
  28.                 $teamId $this->connection->fetchOne("SELECT idequipe FROM equipe WHERE idequipeservice_equipe = {$akaElementId}");
  29.                 if (!$teamId) {
  30.                     continue;
  31.                 }
  32.                 $this->connection->executeQuery("UPDATE aka SET team_id = {$teamId} WHERE idaka = {$akaId}");
  33.             }
  34.         }
  35.         $this->addSql('ALTER TABLE aka ADD CONSTRAINT FK_AE89BA7296CD8AE FOREIGN KEY (team_id) REFERENCES equipe (idequipe)');
  36.         $this->addSql('CREATE INDEX IDX_AE89BA7296CD8AE ON aka (team_id)');
  37.     }
  38.     public function down(Schema $schema): void
  39.     {
  40.         // this down() migration is auto-generated, please modify it to your needs
  41.         $this->addSql('ALTER TABLE aka DROP FOREIGN KEY FK_AE89BA7296CD8AE');
  42.         $this->addSql('DROP INDEX IDX_AE89BA7296CD8AE ON aka');
  43.         $this->addSql('ALTER TABLE aka DROP team_id');
  44.     }
  45. }