migrations/Version20221027114125.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 Version20221027114125 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return '
  14.             Permet de renommer les matricules des etablissements dupliqués. Le nouveau nom est sous le format
  15.             ancienNom-d-n avec n = random(0, 100)
  16.         ';
  17.     }
  18.     public function up(Schema $schema): void
  19.     {
  20.         // this up() migration is auto-generated, please modify it to your needs
  21.         $duplicateNames $this->connection->executeQuery(
  22.             'SELECT COUNT(s.id) as nb, s.registration as school_registration
  23.                 FROM school s
  24.                 group by school_registration
  25.                 having nb > 1'
  26.         )->fetchAllAssociative();
  27.         foreach ($duplicateNames as $item) {
  28.             $registration $item['school_registration'];
  29.             $ids $this->connection->executeQuery('SELECT s2.id FROM school s2 WHERE s2.registration = ?', [$registration])
  30.                 ->fetchAllAssociative();
  31.             foreach ($ids as $id) {
  32.                 $this->addSql(
  33.                     'UPDATE school s1 SET s1.registration = ? WHERE s1.id = ?',
  34.                     [
  35.                         $registration '-d-' rand(0100),
  36.                         $id['id']
  37.                     ]
  38.                 );
  39.             }
  40.         }
  41.     }
  42.     public function down(Schema $schema): void
  43.     {
  44.         // this down() migration is auto-generated, please modify it to your needs
  45.     }
  46. }