Description
Description
I know this topic has been discussed before some long time ago for versions 3 and 4. But I think it is important enough to bring it up again, and hopefully it won't be brushed off as before.
TL;DR; It is my belief that accessors are not usable currently.
I know, it's a bold statement, but I'm happy to be proven wrong. I cannot imagine a single case in which using an accessor on an existing database field (so not creating a virtual field) in the current form would be usable.
The issue is that the accessors/getters are executed on save. This means the value from the database is modified when presented to the application, and is modified in the same way again when being saved back into the database. This guarantees that the data cannot remain consistent no matter how much you try. It is impossible to enter a value for such a field, save it in the database, and then read back the value that you entered. For this reason I also really doubt that there exist any software in the wild that use accessors (excluding virtual fields.)
I think this problem is major enough that a solution should be implemented. In my opinion we should scrap the current logic and simply not call accessors on save (after all, preparing the data to be persisted should be the concern of a mutator, not of an accessor). However, if you are worried that such a change would break existing software (again, I strongly doubt any software using this "feature" actually exists, but regardless), I suggest a safer option: add an extra parameter on the accessor, specifying if this call is meant for data persisting or not. This would allow developers to implement accessors but return the unmodified value when persisting (kind of a backwards way to do it, but at least would work)
The specific use case I am dealing with right now is persisting an encrypted field. I want the field to be encrypted in the database, but accessible as plain text when handling the entity. I can do the encryption with a mutator just fine, but it has no effect if the accessor decrypting the data is called on save...
CakePHP Version
No response