|
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 Version20210424131056 extends AbstractMigration |
|
14
|
|
|
{ |
|
15
|
|
|
public function getDescription() : string |
|
16
|
|
|
{ |
|
17
|
|
|
return ''; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function up(Schema $schema) : void |
|
21
|
|
|
{ |
|
22
|
|
|
// this up() migration is auto-generated, please modify it to your needs |
|
23
|
|
|
$this->addSql('CREATE TABLE image_tag (image_id INT NOT NULL, tag_id INT NOT NULL, INDEX IDX_5B6367D03DA5256D (image_id), INDEX IDX_5B6367D0BAD26311 (tag_id), PRIMARY KEY(image_id, tag_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); |
|
24
|
|
|
$this->addSql('ALTER TABLE image_tag ADD CONSTRAINT FK_5B6367D03DA5256D FOREIGN KEY (image_id) REFERENCES image (id) ON DELETE CASCADE'); |
|
25
|
|
|
$this->addSql('ALTER TABLE image_tag ADD CONSTRAINT FK_5B6367D0BAD26311 FOREIGN KEY (tag_id) REFERENCES tag (id) ON DELETE CASCADE'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function down(Schema $schema) : void |
|
29
|
|
|
{ |
|
30
|
|
|
// this down() migration is auto-generated, please modify it to your needs |
|
31
|
|
|
$this->addSql('DROP TABLE image_tag'); |
|
32
|
|
|
} |
|
33
|
|
|
public function postUp(Schema $schema): void |
|
34
|
|
|
{ |
|
35
|
|
|
$images = $this->connection->fetchAllAssociative('SELECT id, keywords FROM image'); |
|
36
|
|
|
// First, gather unique keywords |
|
37
|
|
|
$allKeywords = []; |
|
38
|
|
|
foreach ($images as $image) { |
|
39
|
|
|
$keywords = unserialize($image['keywords']); |
|
40
|
|
|
$allKeywords = array_merge($allKeywords, $keywords); |
|
41
|
|
|
} |
|
42
|
|
|
$unique_keywords = array_unique($allKeywords); |
|
43
|
|
|
$insertTagStmt = $this->connection->prepare('INSERT INTO tag (name) VALUES (:name)'); |
|
44
|
|
|
|
|
45
|
|
|
// Insert our unique tags into the database, recording the ID values |
|
46
|
|
|
// we insert to use when populating image_tag. |
|
47
|
|
|
$tagsToIds = []; |
|
48
|
|
|
foreach ($unique_keywords as $newTag) { |
|
49
|
|
|
$insertTagStmt->bindValue('name', $newTag); |
|
50
|
|
|
$insertTagStmt->executeStatement(); |
|
51
|
|
|
$tagsToIds[$newTag] = $this->connection->lastInsertId(); |
|
52
|
|
|
}; |
|
53
|
|
|
|
|
54
|
|
|
// Now go through the images again, creating relationships to our newly- |
|
55
|
|
|
// created tags from their existing keywords. |
|
56
|
|
|
$insertImageTagStmt = $this->connection->prepare('INSERT INTO image_tag (image_id, tag_id) VALUES (:image_id, :tag_id)'); |
|
57
|
|
|
foreach ($images as $image) { |
|
58
|
|
|
// Uniquify, just in case we had two of the same tag on one image |
|
59
|
|
|
$keywords = array_unique(unserialize($image['keywords'])); |
|
60
|
|
|
foreach ($keywords as $keyword) { |
|
61
|
|
|
$imageId = $image['id']; |
|
62
|
|
|
$tagId = $tagsToIds[$keyword]; |
|
63
|
|
|
$insertImageTagStmt->bindValue('image_id', $imageId); |
|
64
|
|
|
$insertImageTagStmt->bindValue('tag_id', $tagId); |
|
65
|
|
|
$insertImageTagStmt->executeStatement(); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|