I see only examples how to set it for a table and some fields. I would like to set it globally to TRUE for the whole project.
Currently I am hacking vendor/cakephp/cakephp/src/ORM/Rule/IsUnique.php and setting it to TRUE. With each new version of CakePHP it gets overwritten and I have a check in app_local.php where I throw an exception if the value is FALSE. But this is an emergency hack and I would like to do it how it should be done when CakePHP finally offers the option again.
Below the code how I do it now, as mentioned, a hack and not really satisfying:
# Until [allowMultipleNulls] is not set to TRUE by default, I have to check it here each time in order the project works.
$voidIsUnique = new IsUnique([],[]);
$isUnique = new ReflectionClass(IsUnique::class);
$property = $isUnique->getProperty("_options");
$property->setAccessible(TRUE);
if ( !$property->getValue($voidIsUnique) ["allowMultipleNulls"] ) {
die('<span style="background-color: black; color: yellow; font-size: 36px;">' .
'IsUnique::_options ["allowMultipleNulls"] is FALSE. Fix the source code!' .
'</span>'
);
}
$property->setAccessible(FALSE);
Good, and where do I put this validator class? In the Table file isn’t an option because I create the Table files each time the database layout has been changed.
Can I inject it somehow into the Controller class and then apply it with a function to the root Table class?
The whole project is totally generalised and any individual treatment is in the AppController file, I am also using CRUD, so my TablesController files are completely empty.
I still don’t believe this is what I am looking for. I was thinking about an additional setting in the huge return in app_local.php.
When allowMultipleNulls is set there to TRUE so it overrides the setting for the whole project without creating a new rule or whatever.
Application rules are set per table class and therefore each database table you have in your app.
You could make a custom Table class which “sits” between Cake\ORM\Table and your “per table” classes.
In there you can overwrite/extend the buildRules() method to contain your “global” rule.
The final step would be to adjust all your table classes to extend from your custom “in between” Class instead of directly from Cake\ORM\Table
So levarage the OOP functionality here and inherit what you want to have set globally.
With that you could achieve such a feature you desire. But as @Zuluru already said: This is not something which is configurable by default in CakePHP - just like so many other features because its a framework to enable developers to build their own features.