You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/dto.md
+83-4Lines changed: 83 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Using Data Transfer Objects (DTOs)
2
2
3
-
As stated in [the general design considerations](design.md), in most cases [the DTO pattern](https://en.wikipedia.org/wiki/Data_transfer_object) should be implemented using an API Resource class representing the public data model exposed through the API and [a custom data provider](data-providers.md). In such cases, the class marked with `@ApiResource` will act as a DTO.
3
+
As stated in [the general design considerations](design.md), in most cases [the DTO pattern](https://en.wikipedia.org/wiki/Data_transfer_object) should be implemented using an API Resource class representing the public data model exposed through the API and [a custom data provider](data-providers.md). In such cases, the class marked with `@ApiResource` will act as a DTO.
4
4
5
5
However, it's sometimes useful to use a specific class to represent the input or output data structure related to an operation.
6
6
@@ -81,7 +81,8 @@ We have the following `BookInput`:
81
81
82
82
namespace App\Dto;
83
83
84
-
final class BookInput {
84
+
final class BookInput
85
+
{
85
86
/**
86
87
* @var string
87
88
*/
@@ -110,6 +111,7 @@ final class BookInputDataTransformer implements DataTransformerInterface
110
111
{
111
112
$book = new Book();
112
113
$book->isbn = $data->isbn;
114
+
113
115
return $book;
114
116
}
115
117
@@ -148,7 +150,8 @@ To manage the output, it's exactly the same process. For example, we have the fo
148
150
149
151
namespace App\Dto;
150
152
151
-
final class BookOutput {
153
+
final class BookOutput
154
+
{
152
155
/**
153
156
* @var string
154
157
*/
@@ -177,6 +180,7 @@ final class BookOutputDataTransformer implements DataTransformerInterface
177
180
{
178
181
$output = new BookOutput();
179
182
$output->name = $data->name;
183
+
180
184
return $output;
181
185
}
182
186
@@ -219,7 +223,8 @@ With the following `BookInput`:
219
223
220
224
namespace App\Dto;
221
225
222
-
final class BookInput {
226
+
final class BookInput
227
+
{
223
228
/**
224
229
* @var \App\Entity\Author
225
230
*/
@@ -248,6 +253,7 @@ final class BookInputDataTransformer implements DataTransformerInterface
In order to be able to do a partial update (`PATCH`), it is needed to initialize the input DTO with the existing data before the deserialization process.
286
+
287
+
This way, the input DTO will be correctly validated with its old data and partial new data.
288
+
289
+
Create a class implementing the `DataTransformerInitializerInterface` instead of the `DataTransformerInterface`:
0 commit comments