Skip to content

Commit e425c75

Browse files
authored
Revise child workflow documentation and examples
Updated child workflow execution examples and added new code snippets for handling child workflow IDs.
1 parent 76bd5fd commit e425c75

File tree

1 file changed

+64
-14
lines changed

1 file changed

+64
-14
lines changed

docs/features/child-workflows.md

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ class ParentWorkflow extends Workflow
3838
public function execute()
3939
{
4040
$childPromise = ChildWorkflowStub::make(ChildWorkflow::class);
41-
41+
4242
$childHandle = $this->child();
43-
43+
4444
$childHandle->approve('approved');
45-
45+
4646
$result = yield $childPromise;
47-
47+
4848
return $result;
4949
}
5050
}
@@ -65,15 +65,15 @@ class ParentWorkflow extends Workflow
6565
$child1 = ChildWorkflowStub::make(ChildWorkflow::class, 'first');
6666
$child2 = ChildWorkflowStub::make(ChildWorkflow::class, 'second');
6767
$child3 = ChildWorkflowStub::make(ChildWorkflow::class, 'third');
68-
68+
6969
$childHandles = $this->children();
70-
70+
7171
foreach ($childHandles as $childHandle) {
7272
$childHandle->approve('approved');
7373
}
74-
74+
7575
$results = yield ChildWorkflowStub::all([$child1, $child2, $child3]);
76-
76+
7777
return $results;
7878
}
7979
}
@@ -106,17 +106,17 @@ class ParentWorkflow extends Workflow
106106
public function execute()
107107
{
108108
$childPromise = ChildWorkflowStub::make(ChildWorkflow::class);
109-
109+
110110
$childHandle = $this->child();
111-
111+
112112
yield WorkflowStub::await(fn () => $this->receivedApproval);
113-
113+
114114
if ($childHandle && $this->approvalStatus) {
115115
$childHandle->processApproval($this->approvalStatus);
116116
}
117-
117+
118118
$result = yield $childPromise;
119-
119+
120120
return $result;
121121
}
122122
}
@@ -129,7 +129,57 @@ Always call `$this->child()` or `$this->children()` in the `execute()` method. N
129129
You can access the underlying stored workflow ID using the `id()` method. This allows you to store the ID for external systems to signal the child directly.
130130

131131
```php
132-
yield ActivityStub::make(StoreWorkflowIdActivity::class, $this->child()->id());
132+
use Workflow\ActivityStub;
133+
use Workflow\ChildWorkflowStub;
134+
use Workflow\Workflow;
135+
136+
class ParentWorkflow extends Workflow
137+
{
138+
public function execute()
139+
{
140+
$child = ChildWorkflowStub::make(ChildWorkflow::class);
141+
yield ActivityStub::make(StoreWorkflowIdActivity::class, $this->child()->id());
142+
yield $child;
143+
}
144+
}
145+
```
146+
147+
or
148+
149+
```php
150+
use Workflow\ChildWorkflowStub;
151+
use Workflow\QueryMethod;
152+
use Workflow\Workflow;
153+
154+
class ParentWorkflow extends Workflow
155+
{
156+
private ?int $childId = null;
157+
158+
#[QueryMethod]
159+
public function getChildId(): ?int
160+
{
161+
return $this->childId;
162+
}
163+
164+
public function execute()
165+
{
166+
$child = ChildWorkflowStub::make(ChildWorkflow::class);
167+
$childHandle = $this->child();
168+
yield WorkflowStub::await(fn () => !is_null($childHandle));
169+
$this->childId = $childHandle->id();
170+
yield $child;
171+
}
172+
}
173+
```
174+
175+
NOTE: When using query methods in the same workflow with child handles, you must first await for the child handle to be available in order to make it replay safe.
176+
177+
Then you can interact with the child workflow directly.
178+
179+
```php
180+
$workflow = WorkflowStub::load($workflowId);
181+
$childWorkflow = WorkflowStub::load($workflow->getChildId());
182+
$childWorkflow->approve('approved');
133183
```
134184

135185
## Async Activities

0 commit comments

Comments
 (0)