m190413_125316_relations   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 71
ccs 0
cts 52
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B safeUp() 0 55 3
A safeDown() 0 4 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-element-lists/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-element-lists/
7
 */
8
9
namespace flipbox\craft\element\lists\migrations;
10
11
use Craft;
12
use craft\db\Migration;
13
use flipbox\craft\element\lists\records\Association;
14
use yii\db\Query;
15
16
class m190413_125316_relations extends Migration
17
{
18
    /**
19
     * @return bool
20
     * @throws \craft\errors\SiteNotFoundException
21
     * @throws \yii\db\Exception
22
     */
23
    public function safeUp()
24
    {
25
        // By default, remove siteId value if it matches the primary site (to mirror what native relations is doing)
26
        $primarySiteId = Craft::$app->getSites()->getPrimarySite()->id;
27
        $siteSelect = '(case when e.siteId = '.$primarySiteId.' then null else e.siteId end) sourceSiteId';
28
        $joinOn = 'e.fieldId=e.fieldId AND e.sourceId=r.sourceId AND e.targetId=r.targetId';
29
30
        // Handle multi-site differently.  Match explicitly.
31
        if (Craft::$app->getIsMultiSite()) {
32
            $siteSelect = 'e.siteId';
33
            $joinOn .= ' AND e.siteId=r.sourceSiteId';
34
        }
35
36
        $query = (new Query())
37
            ->from(['{{%elementlist}} e'])
38
            ->leftJoin(
39
                Association::tableName() . ' r',
40
                $joinOn
41
            )
42
            ->select([
43
                'e.fieldId',
44
                'e.sourceId',
45
                'e.targetId',
46
                'e.sortOrder',
47
                'e.dateCreated',
48
                'e.dateUpdated',
49
                'e.uid',
50
                $siteSelect
51
            ])
52
            ->andWhere([
53
                'r.id' => null
54
            ]);
55
56
        foreach ($query->batch(1000) as $batch) {
57
            Craft::$app->getDb()->createCommand()->batchInsert(
58
                Association::tableName(),
59
                [
60
                    'fieldId',
61
                    'sourceId',
62
                    'targetId',
63
                    'sortOrder',
64
                    'dateCreated',
65
                    'dateUpdated',
66
                    'uid',
67
                    'sourceSiteId'
68
                ],
69
                $batch,
70
                false
71
            )->execute();
72
        }
73
74
        $this->dropTableIfExists('elementlist');
75
76
        return true;
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    public function safeDown()
83
    {
84
        return true;
85
    }
86
}
87