Cakephp 3.* shows blank instead of 0


#1

Here is a line of code from a list view:

    <td style="width:20px;"><?php echo $row->adopted; ?></td>

The field adopted is tinyint, should be a 0 or 1 only. This same line shows correct in laravel, a 0 (zero). However in cakephp using the query builder the field is blank.

I had to modify the code to:

    <td style="width:20px;"><?php echo ($row->adopted == 1 ? 1 : 0); ?></td>

to get it to show a 0. Now why in the world is this happening in cakephp 3.x only? Is it something to do with the querybuilder?

It is a test database I use so also in yii2, same line of code show correctly. Exact same database. And yii2 and laravel the querybuilder was used.

I did a quick test using database instead of querybuilder:

    $connection = ConnectionManager::get('default');
    $query = $connection->execute('SELECT * FROM dc_dogs')->fetchAll('obj');

And those results show correctly the 0 (zero). Is there a “bug” in querybuilder?

But I know some people are set in their ways, and this type of thing won’t change.


#2

tintInt with length 1 is mapped to boolean:


#3

All well and good, but if I

    $connection = ConnectionManager::get('default');
    $query = $connection->execute('SELECT * FROM dc_dogs')->fetchAll('obj');

Correct results are given.
So in querybuilder the developers manipulated the way results are returned instead of letting mysql just return natural as is results. Makes no sense.

If null is stored then null should be returned, but if 0 (zero) is stored then 0 should be returned, ditto for 1 (one). What gives anyone the right to hack up mysql returned results? What is wrong with letting itself return what is actually stored, then the view code can be formatted as needed.

By the way that is how other very popular and good frameworks do things.


#4

i dont know why they did it this way, i suspect because its how Phinx did it:


#5

I would be scared to find out what other data is not returned correctly. Why imagine just letting PDO and MYSQL do their thing, what a World it would be.
So to make sure I understand, is this correct?

  • A Mysql querybuilder class was written by someone to return wrong results
  • I have to undo hack to get what’s actually stored

Okay.


#6

MySql wasnt ‘written by someone to return wrong results’ they had to make compromise either support type (bool) that this database doesnt support or make users implement hacks to support boolean

you can

  1. change column length to other than 1
  2. map schema column in your app to int

#7

I am aware of the way mysql and bool works, my point it should be up to the developer to handle such things, as an example I know if dealing with a checkbox to do something like:

<input type="checkbox" name="adopted" id="adopted" value="1"<?php echo ($cat->adopted == 1 ? ' checked' : ''); ?>>

A 0 (zero) is also a boolean as is 1 (one).

  • true or false
  • yes or no
  • 1 or 0

My point also is sometimes you want to see that 0 returned, you only want to see a blank if null is actually stored. There should have been a setting for this.


#8

for input you should use cake formHelper, as for types you can always cast it to (int)

echo (int)true;

gives you 1


#9

But the weird part as I stated already:

Works correctly and displays the 0 (zero). Doesn’t it make you wonder if the cake developers were so concerned about boolean returns, then why does native database return the actual value stored.

Sorry as I can be, this having querybuilder different from database just doesn’t make sense.

I know cakephp is open source and free so I can’t really complain. But I even created an issue and it was just dismissed basically.

But the bottom line is if a 0 is stored you should retrieve a 0. And if it’s a checkbox, then the developer should know how to handle a checkbox as displayed above.

EDIT: I just looked at formhelper it shows

Blockquote
boolean, tinyint(1)
checkbox

Which a checkbox I do display on user form, but the admin page I just display a 0 or 1 in the listview (table view). No need to have a checkbox displayed there. I suppose I could, may even play around with that.

But thanks for all your help. :smiley: