Skip to content

Commit d2a6272

Browse files
committed
allow rename of workflow
1 parent 4b3988d commit d2a6272

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/models/Status.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function beforeSave($insert)
146146
*/
147147
public function afterSave($insert, $changedAttributes)
148148
{
149-
if (!$this->workflow->initial_status_id) {
149+
if ($this->workflow && !$this->workflow->initial_status_id) {
150150
$this->workflow->initial_status_id = $this->id;
151151
$this->workflow->save(false, ['initial_status_id']);
152152
}

src/models/Workflow.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*
1414
* @property Status[] $statuses
1515
* @property Status $initialStatus
16+
* @property Transition[] $transitions
17+
* @property Metadata[] $metadatas
1618
*/
1719
class Workflow extends ActiveRecord
1820
{
@@ -63,13 +65,54 @@ public function getInitialStatus()
6365
return $this->hasOne(Status::className(), ['id' => 'initial_status_id']);
6466
}
6567

68+
/**
69+
* @return \yii\db\ActiveQuery
70+
*/
71+
public function getTransitions()
72+
{
73+
return $this->hasMany(Transition::className(), ['workflow_id' => 'id']);
74+
}
75+
76+
/**
77+
* @return \yii\db\ActiveQuery
78+
*/
79+
public function getMetadatas()
80+
{
81+
return $this->hasMany(Metadata::className(), ['workflow_id' => 'id']);
82+
}
83+
84+
/**
85+
* @inheritdoc
86+
*/
87+
public function beforeSave($insert)
88+
{
89+
if (!$insert && $this->id != $this->oldAttributes['id']) {
90+
$id = $this->id;
91+
$this->id = $this->oldAttributes['id'];
92+
foreach ($this->statuses as $status) {
93+
$status->workflow_id = $id;
94+
$status->save(false, ['workflow_id']);
95+
}
96+
foreach ($this->transitions as $transition) {
97+
$transition->workflow_id = $id;
98+
$transition->save(false, ['workflow_id']);
99+
}
100+
foreach ($this->metadatas as $metadata) {
101+
$metadata->workflow_id = $id;
102+
$metadata->save(false, ['workflow_id']);
103+
}
104+
$this->id = $id;
105+
}
106+
return parent::beforeSave($insert);
107+
}
108+
66109
/**
67110
* @inheritdoc
68111
*/
69112
public function beforeDelete()
70113
{
71114
$this->initial_status_id = null;
72-
$this->save(false,['initial_status_id']);
115+
$this->save(false, ['initial_status_id']);
73116
foreach ($this->statuses as $status) {
74117
$status->delete();
75118
}

0 commit comments

Comments
 (0)