<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20220404222228 extends AbstractMigration
{
public function getDescription(): string
{
return 'ManyToOne Player.country1 and Player.country2';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->connection->executeQuery("UPDATE joueur SET date_naissance_joueur = NULL WHERE date_naissance_joueur LIKE '%0000-00-00%'");
$countries = $this->connection->fetchAllAssociative('SELECT idpays FROM pays');
$countriesIds = array_column($countries, 'idpays');
$lastId = 0;
while (true) {
$players = $this->connection->fetchAllAssociative("SELECT idjoueur, idpays1_joueur, idpays2_joueur FROM joueur WHERE idjoueur > {$lastId} ORDER BY idjoueur ASC LIMIT 1000");
if (empty($players)) {
break;
}
foreach ($players as $player) {
$playerId = $lastId = $player['idjoueur'];
$removeId1 = $removeId2 = false;
if (null !== $player['idpays1_joueur'] && !\in_array($player['idpays1_joueur'], $countriesIds)) {
$removeId1 = true;
}
if (null !== $player['idpays2_joueur'] && !\in_array($player['idpays2_joueur'], $countriesIds)) {
$removeId2 = true;
}
if (!$removeId1 && !$removeId2) {
continue;
}
if ($removeId1 && $removeId2) {
$query = 'idpays1_joueur = NULL, idpays2_joueur = NULL';
} elseif ($removeId1) {
$query = 'idpays1_joueur = NULL';
} elseif ($removeId2) {
$query = 'idpays2_joueur = NULL';
}
$this->connection->executeQuery("UPDATE joueur SET {$query} WHERE idjoueur = {$playerId}");
}
}
$this->addSql('ALTER TABLE joueur ADD CONSTRAINT FK_FD71A9C59B51E2F3 FOREIGN KEY (idpays1_joueur) REFERENCES pays (idpays) ON DELETE SET NULL');
$this->addSql('ALTER TABLE joueur ADD CONSTRAINT FK_FD71A9C515DEE510 FOREIGN KEY (idpays2_joueur) REFERENCES pays (idpays) ON DELETE SET NULL');
$this->addSql('CREATE INDEX IDX_FD71A9C59B51E2F3 ON joueur (idpays1_joueur)');
$this->addSql('CREATE INDEX IDX_FD71A9C515DEE510 ON joueur (idpays2_joueur)');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE joueur DROP FOREIGN KEY FK_FD71A9C59B51E2F3');
$this->addSql('ALTER TABLE joueur DROP FOREIGN KEY FK_FD71A9C515DEE510');
$this->addSql('DROP INDEX IDX_FD71A9C59B51E2F3 ON joueur');
$this->addSql('DROP INDEX IDX_FD71A9C515DEE510 ON joueur');
}
}