cakephp
cakephp copied to clipboard
Alternative to requireFieldPresence
Description
The newly introduced requireFieldPresence for entities breaks a lot of code including core, so it cannot be used ATM. One example is the translate behavior and I am sure lots of other places.
The fix is usually something like:
$entity->has($property) ? $entity->get($property) : null;
One solution is to introduce a getOrFail() method and/or a getIfExists() method which are both independent of requireFieldPresence so the callers do not need to worry about the setting or have to use code like the above.
CakePHP Version
No response
Having both the proposed methods and requireFieldPresence seems redundant. We may also want to deprecate requireFieldPresence as well.
I think it's a good idea to deprecate it, before it's actually used, because the current implementation creates more problems than it solves.
An alternative which is a slightly breaking change is that it is limited to property access (ie. in EntityTrait::__get) so that it will basically only help the application ensure the entities are baked correctly.
I can cook up an RFC PR if you want to see it practice.
@markstory I added the PR with the suggested change. What do you think?
This issue is stale because it has been open for 120 days with no activity. Remove the stale label or comment or this will be closed in 15 days
https://github.com/cakephp/cakephp/pull/18216 merged