Skip to content

Commit 17a9c2b

Browse files
committed
docs: update docs to use transformRelated()
1 parent 249163f commit 17a9c2b

5 files changed

Lines changed: 54 additions & 7 deletions

File tree

‎user_guide_src/source/outgoing/api_transformers.rst‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ resource name. Inside these methods, you can access the current resource being t
147147

148148
.. literalinclude:: api_transformers/009.php
149149

150+
.. important::
151+
When transforming related resources inside an include method, always use ``$this->transformRelated()`` instead of manually instantiating a new transformer (e.g., ``new PostTransformer()``).
152+
153+
The ``transformRelated()`` method provides two major benefits:
154+
155+
1. It automatically determines whether to transform a single item or a collection based on the provided data.
156+
2. It prevents **Global State Leakage**, ensuring that the parent request's query parameters (like ``?include=`` or ``?fields=``) do not accidentally bleed into the child transformer and cause unexpected infinite loops or `ApiException`.
157+
150158
Note how the include methods use ``$this->resource['id']`` to access the ID of the user being transformed.
151159
The ``$this->resource`` property is automatically set by the transformer when ``transform()`` is called.
152160

@@ -301,6 +309,20 @@ Class Reference
301309

302310
.. literalinclude:: api_transformers/019.php
303311

312+
.. php:method:: transformRelated(string|TransformerInterface $transformer, mixed $resources)
313+
314+
:param class-string<TransformerInterface>|TransformerInterface $transformer: The class name or instance of the transformer to use
315+
:param mixed $resources: The related resource data (Entity, array, object, or collection of these)
316+
:returns: The transformed array (either a single item or a collection)
317+
:rtype: array
318+
319+
Transforms related resources safely by isolating the new transformer from the parent's request state.
320+
This prevents nested transformers from incorrectly inheriting the ``fields`` or ``includes`` requested for the parent resource.
321+
322+
It automatically uses ``transform()`` for single resources and ``transformMany()`` for collections (lists) by inspecting the provided ``$resources``.
323+
324+
.. literalinclude:: api_transformers/024.php
325+
304326
.. php:method:: getAllowedFields()
305327
306328
:returns: Array of allowed field names, or ``null`` to allow all fields

‎user_guide_src/source/outgoing/api_transformers/009.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ protected function includePosts(): array
2020
// Use $this->resource to access the current resource being transformed
2121
$posts = model('PostModel')->where('user_id', $this->resource['id'])->findAll();
2222

23-
return (new PostTransformer())->transformMany($posts);
23+
return $this->transformRelated(PostTransformer::class, $posts);
2424
}
2525

2626
protected function includeComments(): array
2727
{
2828
$comments = model('CommentModel')->where('user_id', $this->resource['id'])->findAll();
2929

30-
return (new CommentTransformer())->transformMany($comments);
30+
return $this->transformRelated(CommentTransformer::class, $comments);
3131
}
3232
}

‎user_guide_src/source/outgoing/api_transformers/010.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ protected function includePosts(): array
2727
{
2828
$posts = model('PostModel')->where('user_id', $this->resource['id'])->findAll();
2929

30-
return (new PostTransformer())->transformMany($posts);
30+
return $this->transformRelated(PostTransformer::class, $posts);
3131
}
3232

3333
protected function includeComments(): array
3434
{
3535
$comments = model('CommentModel')->where('user_id', $this->resource['id'])->findAll();
3636

37-
return (new CommentTransformer())->transformMany($comments);
37+
return $this->transformRelated(CommentTransformer::class, $comments);
3838
}
3939

4040
protected function includeOrders(): array
@@ -43,6 +43,6 @@ protected function includeOrders(): array
4343
// because 'orders' is not in getAllowedIncludes()
4444
$orders = model('OrderModel')->where('user_id', $this->resource['id'])->findAll();
4545

46-
return (new OrderTransformer())->transformMany($orders);
46+
return $this->transformRelated(OrderTransformer::class, $orders);
4747
}
4848
}

‎user_guide_src/source/outgoing/api_transformers/023.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ protected function includePosts(): array
2626
{
2727
$posts = model('PostModel')->where('user_id', $this->resource['id'])->findAll();
2828

29-
return (new PostTransformer())->transformMany($posts);
29+
return $this->transformRelated(PostTransformer::class, $posts);
3030
}
3131

3232
protected function includeOrders(): array
3333
{
3434
// This method exists but cannot be called via the API
3535
$orders = model('OrderModel')->where('user_id', $this->resource['id'])->findAll();
3636

37-
return (new OrderTransformer())->transformMany($orders);
37+
return $this->transformRelated(OrderTransformer::class, $orders);
3838
}
3939
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Transformers;
4+
5+
use CodeIgniter\API\BaseTransformer;
6+
7+
class UserTransformer extends BaseTransformer
8+
{
9+
public function toArray(mixed $resource): array
10+
{
11+
return [
12+
'id' => $resource['id'],
13+
'name' => $resource['name'],
14+
'email' => $resource['email'],
15+
];
16+
}
17+
18+
protected function includePosts(): array
19+
{
20+
// Use $this->resource to access the current resource being transformed
21+
$posts = model('PostModel')->where('user_id', $this->resource['id'])->findAll();
22+
23+
return $this->transformRelated(PostTransformer::class, $posts);
24+
}
25+
}

0 commit comments

Comments
 (0)