# Database nullable integer field has value 'NULL', cakephp displaying '0'

**URL:** <https://discourse.cakephp.org/t/database-nullable-integer-field-has-value-null-cakephp-displaying-0/10082>\
**Category:** Uncategorized\
**Created:** [January 16, 2022, 11:17am UTC](https://discourse.cakephp.org/t/database-nullable-integer-field-has-value-null-cakephp-displaying-0/10082 "2022-01-16T11:17:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![drtrppr](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/drtrppr/32/1763_2.png) [@drtrppr](https://discourse.cakephp.org/u/drtrppr)\
**Post date:** [January 16, 2022, 11:17am UTC](https://discourse.cakephp.org/t/database-nullable-integer-field-has-value-null-cakephp-displaying-0/10082/1 "2022-01-16T11:17:00Z")

</div>

How to get cakephp just to display what’s in the database, I’m a bit surprised, expect that to be the default behavior.

Can’t find any answers, as usual, don’t have a clue.

---

<div class="post-metadata">

**Author:** ![Jawfin](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Jawfin](https://discourse.cakephp.org/u/Jawfin)\
**Post date:** [January 16, 2022, 2:05pm UTC](https://discourse.cakephp.org/t/database-nullable-integer-field-has-value-null-cakephp-displaying-0/10082/2 "2022-01-16T14:05:44Z")

</div>

A few posts down on this forum you’ll find this discussion: -

> [@Empty string in database instead of NULL](https://discourse.cakephp.org/t/empty-string-in-database-instead-of-null/10039):
>
> If I enter a value from a field that is empty via a form, an entry with an empty string is stored in the database. Could it be NULL instead? Is there anything to set, or is it a PHP CAKE feature? Thank You

It would seem this is an ongoing explanation over many years.  
From what I understand, being that CakePHP is PHP language oriented, it makes sense - but please follow the thoughts there for clarity.

---

<div class="post-metadata">

**Author:** ![drtrppr](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/drtrppr/32/1763_2.png) [@drtrppr](https://discourse.cakephp.org/u/drtrppr)\
**Post date:** [January 16, 2022, 8:37pm UTC](https://discourse.cakephp.org/t/database-nullable-integer-field-has-value-null-cakephp-displaying-0/10082/3 "2022-01-16T20:37:56Z")

</div>

Well, I know there can be a lot of discussion about the difference between NULL, “” and " ", but as far I know, that’s about strings, varchars etc.

When googling, I can only find about character type of fields. IMHO same with post you are mentioning.

In my case, it’s data in an integer defined field in the database.

What puzzles I really can’t find anything. Am I using the wrong search-words or does nobody have this problem? In that case it’s some config of my environment?

---

<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:** [January 16, 2022, 8:52pm UTC](https://discourse.cakephp.org/t/database-nullable-integer-field-has-value-null-cakephp-displaying-0/10082/4 "2022-01-16T20:52:28Z")

</div>

Maybe show the code that you are using to display the field in question?

---

<div class="post-metadata">

**Author:** ![drtrppr](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/drtrppr/32/1763_2.png) [@drtrppr](https://discourse.cakephp.org/u/drtrppr)\
**Post date:** [January 17, 2022, 10:22am UTC](https://discourse.cakephp.org/t/database-nullable-integer-field-has-value-null-cakephp-displaying-0/10082/5 "2022-01-17T10:22:57Z")

</div>

![2022-01-17-10-55-57_scrot](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/3/32816c6c1ef72490ddc99a6b73d157b011935bdb.png)

![2022-01-17-10-54-40_scrot](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/b/b39426ada5703a2e100d73b651495a7792800287.png)

 ![2022-01-17-10-55-22_scrot](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/2/2ea657132063810f42e40d36c7f7b45baca00dcb.png)

![2022-01-17-11-18-08_scrot](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/8/8665af3ebba0df66fd562ff0ce479a0626b9a0c4.png)

Just fresh baked code.

Read the [NumberHelper section](https://book.cakephp.org/4/en/core-libraries/number.html), but no clue.

---

<div class="post-metadata">

**Author:** ![ADmad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/admad/32/20_2.png) [@ADmad](https://discourse.cakephp.org/u/ADmad)\
**Post date:** [January 17, 2022, 1:17pm UTC](https://discourse.cakephp.org/t/database-nullable-integer-field-has-value-null-cakephp-displaying-0/10082/6 "2022-01-17T13:17:10Z")

</div>

Since `NumberHelper::format()` is used for the values it’s no surprise that you get `0` for `null`. Bake would have to be updated to generate the template code taking into account that the field is nullable. You can open an issue on bake’s repo on Github.

---

<div class="post-metadata">

**Author:** ![drtrppr](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/drtrppr/32/1763_2.png) [@drtrppr](https://discourse.cakephp.org/u/drtrppr)\
**Post date:** [January 18, 2022, 9:32am UTC](https://discourse.cakephp.org/t/database-nullable-integer-field-has-value-null-cakephp-displaying-0/10082/7 "2022-01-18T09:32:13Z")

</div>

![Screenshot_2022-01-18_09-27-19](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/0/0e53341879ed867621d73af6d09e13561e2bd2a8.png)

gives me the expected

 ![Screenshot_2022-01-18_10-11-21](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/d/dd397b4522c6d697c8def52bbcda82b7b6604a0d.png)

Looks a bit patchy to me, why doesn’t the NumberHelper deal with it?

But now knowing what the problem is, and googling again, I found the [issue](https://github.com/cakephp/cakephp/issues/8961) and your reply to it, it’s clear to me now.

Apart from opening an issue on bake’s repository, one on cookbook’s seems appropriate too me too (a warning).

---

<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:** [January 18, 2022, 8:12pm UTC](https://discourse.cakephp.org/t/database-nullable-integer-field-has-value-null-cakephp-displaying-0/10082/8 "2022-01-18T20:12:09Z")

</div>

The number helper is for outputting, um, numbers.

---

<div class="post-metadata">

**Author:** ![drtrppr](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/drtrppr/32/1763_2.png) [@drtrppr](https://discourse.cakephp.org/u/drtrppr)\
**Post date:** [January 19, 2022, 12:40pm UTC](https://discourse.cakephp.org/t/database-nullable-integer-field-has-value-null-cakephp-displaying-0/10082/9 "2022-01-19T12:40:26Z")

</div>

Getting some second thoughts about this issue.

In the context of building a prototype app on a dataset with about 200 columns as an afternoon-project, and trying to catch a deadline, I ditched the whole number-helper.

Now NULL-values are displayed like blancs, so I’m rid of the unwanted zero’s. In some columns the NULL’s will have to be blancs, in other columns a ‘-’, ‘?’, ‘\*\*\*\*\*’ or a coloured .

The non-nulls are displayed unformatted, users are now better able to ‘recognize’ displayed values and indicate strange values. Per column they will indicate what the format will be.

So in this case and some other (non-cake) projects I took part, the use of NumberHelper in the baked app is more of a burden then a help. A ‘bare’ baked version would make more sense, and again, in my cases. Since the numberhelper is in the baked code, there will be enough reasons to do so.

And I guess I’m in need of a helper in which the NULL’s can be configurated (display NULL like blanc, ?, \* etc.). That will be like ADmad wrote?

> The engine used by `NumberHelper` is configurable so if you want `NumberHelper::format()` to behave differently use your own engine.

All that will be of later concern, if needed. Just wrote this down in case I stumble on the same issue next year or so.

---

<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:** [January 19, 2022, 9:10pm UTC](https://discourse.cakephp.org/t/database-nullable-integer-field-has-value-null-cakephp-displaying-0/10082/10 "2022-01-19T21:10:48Z")

</div>

Without going to much into detail but you could write your own helper / extend the NumberHelper to do exactly what you want.

---

<div class="post-metadata">

**Author:** ![drtrppr](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/drtrppr/32/1763_2.png) [@drtrppr](https://discourse.cakephp.org/u/drtrppr)\
**Post date:** [January 20, 2022, 10:13am UTC](https://discourse.cakephp.org/t/database-nullable-integer-field-has-value-null-cakephp-displaying-0/10082/11 "2022-01-20T10:13:32Z")

</div>

Yep, extend number-method like the zero-option in the currency method:

```auto
zero
	

The text to use for zero values; can be a string or a number. ie. 0, ‘Free!’.

```

And then read ‘null’ instead of ‘zero’, and ‘free’ for with ‘not available’.
