m170720_124351_alter_table_report_add_validator_id.php 1.55 KB
Newer Older
Marcello Pivanti's avatar
Marcello Pivanti committed
1
2
3
4
5
6
7
<?php

/**
 * Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
 * OPEN 2.0
 *
 *
waltermazza's avatar
waltermazza committed
8
 * @package    arter\amos\report\migrations
Marcello Pivanti's avatar
Marcello Pivanti committed
9
10
11
12
 * @category   Migration
 * @author     Elite Division S.r.l.
 */

waltermazza's avatar
waltermazza committed
13
use arter\amos\report\models\Report;
Marcello Pivanti's avatar
Marcello Pivanti committed
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
use yii\db\Migration;

/**
 * Class m170720_124351_alter_table_report_add_validator_id
 */
class m170720_124351_alter_table_report_add_validator_id extends Migration
{
    public function safeUp()
    {
        $this->addColumn(Report::tableName(), 'validator_id', $this->integer()->null()->defaultValue(null)->after('creator_id'));

        $reports = Report::find()->all();
        foreach ($reports as $report){
            $className = $report->classname;
            $contentModel = $className::findOne($report->context_id);
            if(!empty($contentModel)) {
                $report->creator_id = $contentModel->created_by;
                if (!is_null($contentModel->getBehavior('workflowLog')) && $contentModel->hasMethod('getValidatedStatus')) {
                    $validatorId = $contentModel->getStatusLastUpdateUser($contentModel->getValidatedStatus());
                    $report->validator_id = $validatorId;
                }
                $report->save();
            }
        }
        return true;

    }

    public function safeDown()
    {

        $reports = Report::find()->all();
        foreach ($reports as $report){
            $report->creator_id = $report->created_by;
            $report->save();
        }
        $this->dropColumn(Report::tableName(), 'validator_id');

        return true;
    }

}