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

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.

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

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.

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?

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




Just fresh baked code.

Read the NumberHelper section, but no clue.

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.

gives me the expected

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 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).

The number helper is for outputting, um, numbers.

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.

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

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


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’.