cakephp icon indicating copy to clipboard operation
cakephp copied to clipboard

Alternative to requireFieldPresence

Open nicosp opened this issue 8 months ago • 3 comments
trafficstars

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

nicosp avatar Feb 26 '25 09:02 nicosp

Having both the proposed methods and requireFieldPresence seems redundant. We may also want to deprecate requireFieldPresence as well.

markstory avatar Feb 26 '25 18:02 markstory

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.

nicosp avatar Feb 27 '25 07:02 nicosp

@markstory I added the PR with the suggested change. What do you think?

nicosp avatar Mar 17 '25 07:03 nicosp

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

github-actions[bot] avatar Jul 28 '25 00:07 github-actions[bot]

https://github.com/cakephp/cakephp/pull/18216 merged

dereuromark avatar Nov 02 '25 13:11 dereuromark