# Setting "allowMultipleNulls" permanently to TRUE for whole project

**URL:** <https://discourse.cakephp.org/t/setting-allowmultiplenulls-permanently-to-true-for-whole-project/11287>\
**Category:** Need Help\
**Created:** [June 15, 2023, 8:16pm UTC](https://discourse.cakephp.org/t/setting-allowmultiplenulls-permanently-to-true-for-whole-project/11287 "2023-06-15T20:16:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeterVARGA](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/petervarga/32/1928_2.png) [@PeterVARGA](https://discourse.cakephp.org/u/PeterVARGA)\
**Post date:** [June 15, 2023, 8:17pm UTC](https://discourse.cakephp.org/t/setting-allowmultiplenulls-permanently-to-true-for-whole-project/11287/1 "2023-06-15T20:17:00Z")

</div>

I refer to this documentation: [https://book.cakephp.org/4/en/orm/validation.html#creating-unique-field-rules](https://book.cakephp.org/4/en/orm/validation.html#creating-unique-field-rules)

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:

```auto
# 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);

```

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [June 16, 2023, 4:22am UTC](https://discourse.cakephp.org/t/setting-allowmultiplenulls-permanently-to-true-for-whole-project/11287/2 "2023-06-16T04:22:12Z")

</div>

Write your own validator class which extends the default one and makes just this change, then reference that class instead of the default one?

---

<div class="post-metadata">

**Author:** ![PeterVARGA](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/petervarga/32/1928_2.png) [@PeterVARGA](https://discourse.cakephp.org/u/PeterVARGA)\
**Post date:** [June 16, 2023, 6:30am UTC](https://discourse.cakephp.org/t/setting-allowmultiplenulls-permanently-to-true-for-whole-project/11287/3 "2023-06-16T06:30:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [June 16, 2023, 1:32pm UTC](https://discourse.cakephp.org/t/setting-allowmultiplenulls-permanently-to-true-for-whole-project/11287/4 "2023-06-16T13:32:48Z")

</div>

See [Creating reusable validators](https://book.cakephp.org/4/en/core-libraries/validation.html#creating-reusable-validators).

---

<div class="post-metadata">

**Author:** ![PeterVARGA](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/petervarga/32/1928_2.png) [@PeterVARGA](https://discourse.cakephp.org/u/PeterVARGA)\
**Post date:** [June 20, 2023, 9:06am UTC](https://discourse.cakephp.org/t/setting-allowmultiplenulls-permanently-to-true-for-whole-project/11287/5 "2023-06-20T09:06:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [June 20, 2023, 1:43pm UTC](https://discourse.cakephp.org/t/setting-allowmultiplenulls-permanently-to-true-for-whole-project/11287/6 "2023-06-20T13:43:26Z")

</div>

I understand what you’re looking for, but it doesn’t exist. Creating your own validator that does it is the solution.

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [June 20, 2023, 3:44pm UTC](https://discourse.cakephp.org/t/setting-allowmultiplenulls-permanently-to-true-for-whole-project/11287/7 "2023-06-20T15:44:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![PeterVARGA](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/petervarga/32/1928_2.png) [@PeterVARGA](https://discourse.cakephp.org/u/PeterVARGA)\
**Post date:** [June 21, 2023, 9:00pm UTC](https://discourse.cakephp.org/t/setting-allowmultiplenulls-permanently-to-true-for-whole-project/11287/8 "2023-06-21T21:00:48Z")

</div>

Thank you Kevin for your time. I will think about it. There are some internal restrictions I have to sort out.
