Update not create new dto #97
Replies: 4 comments 1 reply
-
I think I have a similar use-case, where I need to update some properties of a data object after it has been created, while also ensuring these properties will be cast to correct types and possibly validated. It doesn't look like there are any setters or update methods, though. The only workaround at the moment I can think of is to convert the existing data object to an array, merge the new values to the array and then create a new data object.
It would be nicer if I could just do:
|
Beta Was this translation helpful? Give feedback.
-
Since the data object is just a plain PHP object, why not do the following? $data = DataObject::from($request);
$data->foo = 12; |
Beta Was this translation helpful? Give feedback.
-
Hi @abrampl and @ragulka public static function fromMultiple(Model $model, DataRequest $dataRequest): self
{
$someNewData = collect($$model->json_column)->merge(collect($DataRequest)->filter())
return new self(
....
);
}
|
Beta Was this translation helpful? Give feedback.
-
As @Nir-An suggets, I think it is better to create a new data object, by using a method which combines the arguments in the creating process it is even easier. By definition, data transfer objects should be immutable, so actually you shouldn't change a DTO once it is created. When you want to change a DTO, then you should create a new one. Which can avoid a lot of problems later on when you're using the DTO's. Now we're not enforcing such a pattern in this package, but it is something to think about. As for the setters, I'm not completely opposed to it so I'm always open for a PR implementing this. |
Beta Was this translation helpful? Give feedback.
-
Helo,
In my application i'v created Data class and also use this class as cast
To update this model i use modal with only one field to update property date. When i construct new data class from request i loose my type property that was previously saved in db. How i can merge partial update to my Data class. NOw this is my code to do this.
Beta Was this translation helpful? Give feedback.
All reactions