-
Notifications
You must be signed in to change notification settings - Fork 502
/
20141105085836_form_attributes_relations.php
85 lines (76 loc) · 2.68 KB
/
20141105085836_form_attributes_relations.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
use Phinx\Migration\AbstractMigration;
class FormAttributesRelations extends AbstractMigration
{
/**
* Migrate Up.
*/
public function up()
{
// add column form_group_di to form_attributes
$this->table('form_attributes')
->addColumn('form_group_id', 'integer', [
'default' => null,
'null' => true,
])
->addForeignKey('form_group_id', 'form_groups', 'id', [
'delete' => 'CASCADE',
])
->update()
;
// migrate all form_attribute <-> form_group relationships
$connection = $this->getAdapter()->getConnection();
$relationships = $this->fetchAll('SELECT * FROM form_groups_form_attributes;');
$prepared_update = $connection->prepare("
UPDATE form_attributes
SET form_group_id = :form_group_id
WHERE id = :id
;");
foreach ($relationships as $r) {
$prepared_update->execute([
':form_group_id' => $r['form_group_id'],
':id' => $r['form_attribute_id'],
]);
}
// drop the pivot table
$this->table('form_groups_form_attributes')->drop()->save();
}
/**
* Migrate Down.
*/
public function down()
{
// create form_groups_form_attributes
$this->table('form_groups_form_attributes', [
'id' => false,
'primary_key' => ['form_group_id', 'form_attribute_id'],
])
->addColumn('form_group_id', 'integer')
->addColumn('form_attribute_id', 'integer')
->addForeignKey('form_group_id', 'form_groups', 'id')
->addForeignKey('form_attribute_id', 'form_attributes', 'id')
->create();
// migrate all form_attribute <-> form_group relationships
$connection = $this->getAdapter()->getConnection();
$form_attributes = $this->fetchAll('SELECT * FROM form_attributes;');
$prepared_insert = $connection->prepare("
INSERT INTO form_groups_form_attributes (
form_group_id, form_attribute_id
) VALUES (
:form_group_id, :form_attribute_id
)
;");
foreach ($form_attributes as $fa) {
$prepared_insert->execute([
':form_group_id' => $fa['form_group_id'],
':form_attribute_id' => $fa['id'],
]);
}
// drop column in form_attributes
$this->table('form_attributes')
->dropForeignKey('form_group_id')
->removeColumn('form_group_id')
->update()
;
}
}