|
13 | 13 | * |
14 | 14 | * @property Status[] $statuses |
15 | 15 | * @property Status $initialStatus |
| 16 | + * @property Transition[] $transitions |
| 17 | + * @property Metadata[] $metadatas |
16 | 18 | */ |
17 | 19 | class Workflow extends ActiveRecord |
18 | 20 | { |
@@ -63,13 +65,54 @@ public function getInitialStatus() |
63 | 65 | return $this->hasOne(Status::className(), ['id' => 'initial_status_id']); |
64 | 66 | } |
65 | 67 |
|
| 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 | + |
66 | 109 | /** |
67 | 110 | * @inheritdoc |
68 | 111 | */ |
69 | 112 | public function beforeDelete() |
70 | 113 | { |
71 | 114 | $this->initial_status_id = null; |
72 | | - $this->save(false,['initial_status_id']); |
| 115 | + $this->save(false, ['initial_status_id']); |
73 | 116 | foreach ($this->statuses as $status) { |
74 | 117 | $status->delete(); |
75 | 118 | } |
|
0 commit comments