|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DoctrineMigrations; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Schema\Schema; |
| 8 | +use Doctrine\Migrations\AbstractMigration; |
| 9 | + |
| 10 | +/** |
| 11 | + * Auto-generated Migration: Please modify to your needs! |
| 12 | + */ |
| 13 | +final class Version20250326151430 extends AbstractMigration |
| 14 | +{ |
| 15 | + public function getDescription(): string |
| 16 | + { |
| 17 | + return 'Convert HTML character entities in excerpt fields to their actual characters'; |
| 18 | + } |
| 19 | + |
| 20 | + public function up(Schema $schema): void |
| 21 | + { |
| 22 | + // Get all records that have excerpt fields containing '&' (indicating HTML entities) |
| 23 | + $qb = $this->connection->createQueryBuilder(); |
| 24 | + $records = $qb->select('e.id', 'e.excerpt') |
| 25 | + ->from('event', 'e') |
| 26 | + ->where($qb->expr()->like('e.excerpt', ':pattern')) |
| 27 | + ->setParameter('pattern', '%&%') |
| 28 | + ->executeQuery() |
| 29 | + ->fetchAllAssociative(); |
| 30 | + |
| 31 | + // Process each record |
| 32 | + foreach ($records as $record) { |
| 33 | + if (null === $record['excerpt']) { |
| 34 | + continue; // Skip if excerpt is null |
| 35 | + } |
| 36 | + |
| 37 | + // Decode HTML entities using PHP's native function |
| 38 | + $decodedExcerpt = html_entity_decode($record['excerpt'], ENT_QUOTES | ENT_HTML5); |
| 39 | + |
| 40 | + // Only update if something changed |
| 41 | + if ($decodedExcerpt !== $record['excerpt']) { |
| 42 | + $this->connection->createQueryBuilder() |
| 43 | + ->update('event', 'e') |
| 44 | + ->set('e.excerpt', ':excerpt') |
| 45 | + ->where('e.id = :id') |
| 46 | + ->setParameter('excerpt', $decodedExcerpt) |
| 47 | + ->setParameter('id', $record['id']) |
| 48 | + ->executeStatement(); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public function down(Schema $schema): void |
| 54 | + { |
| 55 | + // No need to revert this change as it just formats data without functional changes |
| 56 | + } |
| 57 | +} |
0 commit comments