# |
Jul 12th 2021, 19:48 |
kevin.pfeifer |
I am not aware of what the “best practice” for that would be Its just that I am aware of either the controller method or the template (set input value) method |
# |
Jul 12th 2021, 19:48 |
cnizzardini |
behavior is another way to try, thats how the timestamp stuff works I believe |
# |
Jul 12th 2021, 19:47 |
cnizzardini |
kevins approach might be best, its more obvious, idk, you have options though |
# |
Jul 12th 2021, 19:47 |
tyler.adam.lazenby |
I am testing it |
# |
Jul 12th 2021, 19:47 |
cnizzardini |
haven't actually tried using beforeMarshal for this purpose |
# |
Jul 12th 2021, 19:46 |
cnizzardini |
or overwrite an existing value because it was not present in the update |
# |
Jul 12th 2021, 19:46 |
cnizzardini |
wouldn't want to default to a value on an update if the user supplied a value |
# |
Jul 12th 2021, 19:46 |
kevin.pfeifer |
in my opinion |
# |
Jul 12th 2021, 19:46 |
kevin.pfeifer |
i understand why setting default values in the models seems fine but depending on your model/data you could run in the use case, that you need different default values depending on the situation Therefore it would be better to create an empty entity, patch the default value and then patch the input data |
# |
Jul 12th 2021, 19:46 |
cnizzardini |
id do some testing around create vs update with that though |
# |
Jul 12th 2021, 19:45 |
cnizzardini |
https://book.cakephp.org/4/en/orm/saving-data.html#modifying-request-data-before-building-entities |
# |
Jul 12th 2021, 19:44 |
tyler.adam.lazenby |
I am |
# |
Jul 12th 2021, 19:44 |
cnizzardini |
then try beforeMarshall |
# |
Jul 12th 2021, 19:44 |
tyler.adam.lazenby |
That feels "stinky" |
# |
Jul 12th 2021, 19:44 |
cnizzardini |
nothing wrong with doing it in the controller either |
# |
Jul 12th 2021, 19:44 |
tyler.adam.lazenby |
hmmmmm |
# |
Jul 12th 2021, 19:44 |
cnizzardini |
so beforeMarshall could be an option |
# |
Jul 12th 2021, 19:43 |
tyler.adam.lazenby |
```public function _setToken($token = null): string { if (!$token) { return Security::randomString(32); } return $token; }``` |
# |
Jul 12th 2021, 19:43 |
tyler.adam.lazenby |
Dang it, the entity doesn't save it by default |
# |
Jul 12th 2021, 19:42 |
japerlman |
yeah I keep getting the cake 3 docs and it takes me longer than it should to realize |
# |
Jul 12th 2021, 19:42 |
japerlman |
thanks for the help |
# |
Jul 12th 2021, 19:42 |
japerlman |
ok yeah it was the missing use, sorry guys. |
# |
Jul 12th 2021, 19:41 |
cnizzardini |
i suppose it would fail on marshall if not set before |
# |
Jul 12th 2021, 19:41 |
kevin.pfeifer |
i was just looking at https://stackoverflow.com/questions/8344189/cakephp-set-default-field-values-in-the-model |
# |
Jul 12th 2021, 19:40 |
tyler.adam.lazenby |
the entity is the best place for this I think |
# |
Jul 12th 2021, 19:40 |
tyler.adam.lazenby |
I agree with kevin |
# |
Jul 12th 2021, 19:40 |
kevin.pfeifer |
i would guess the default value shoul be present before the save is being triggered |
# |
Jul 12th 2021, 19:40 |
cnizzardini |
im surprised there is no option for this in Entity |
# |
Jul 12th 2021, 19:40 |
kevin.pfeifer |
well, i looked at the cake3 doc, not the cake4…damn google, why do you still rank 3 higher than 4 |
# |
Jul 12th 2021, 19:39 |
cnizzardini |
and set the value of its null or whatever |
# |
Jul 12th 2021, 19:39 |
cnizzardini |
you could use a beforeSave |
# |
Jul 12th 2021, 19:39 |
tyler.adam.lazenby |
I like the idea of a field rule that will be system wide to be on the model and not action specific |
# |
Jul 12th 2021, 19:39 |
tyler.adam.lazenby |
Exactly |
# |
Jul 12th 2021, 19:38 |
cnizzardini |
that seems odd, what if the entity is created elsewhere too? |
# |
Jul 12th 2021, 19:38 |
dereuromark |
It is actually EventInterface :P but no biggy |
# |
Jul 12th 2021, 19:38 |
kevin.pfeifer |
otherwise you could set the default value on the field as well i guess |
# |
Jul 12th 2021, 19:38 |
kevin.pfeifer |
so in the empty entity when your in the add function e.g. |
# |
Jul 12th 2021, 19:38 |
tyler.adam.lazenby |
really? |
# |
Jul 12th 2021, 19:38 |
kevin.pfeifer |
default values should be set in the controller as far as I know |
# |
Jul 12th 2021, 19:37 |
tyler.adam.lazenby |
yeah |
# |
Jul 12th 2021, 19:37 |
cnizzardini |
maybe the entity |