I'm really torn on what to do on this one. For the past 10 years I've just put up with this but I think it's time to decide the right way to handle this issue.
According to the internet, MySQL doesn't support a native datatype, which makes sense why whenever I choose "BOOLEAN" I see the column end up as a tinyint(1).
I have no problem with storing data as a tinyint with a 1 or 0 value, however I do have a problem with the fact that when I select many rows from the database with this column I get a string "1" or "0" instead of a PHP native bool true/false. PHP can still deal with this variable as a boolean value of course as it seems to automatically know what I want it to do whenever I do == 0 or == 1 or even == '0' or == '1' if I'm really feeling up to it.
So, here I go, taking this data and running it into PHP's json_encode
function and outputting it when my Backbone/Handlebars implementation makes an ajax call, and getting a nice beautiful JSON object (usually with many rows' data in it) with this: {"myVariable":"1"}
or this {"myVariable":"0"}
.
I'm sure you can see the issue with wanting to do something like this in Handlebars.js:
{{#if myVariable}}
Do this
{{else}}
Do that
{{/if}}
Now, what I really DON'T want to do is something like this:
<?php
for($i = 0; $i < count($recordSet); $i++) {
$recordSet[$i]['myVariable'] = (bool)$recordSet[$i]['myVariable'];
}
?>
Being that the application I'm building has thousands of columns in the database I can't (or just should need to) just run the above code for every column I want to have act like a bool in my javascript. And I really don't want to do this (although it would probably be easiest)
Handlebars.registerHelper('ifTrue', function(a, b) {
// Cast the compared value to a bool and then run a regular handlebars #if statement
});
Is there a better way? I'm using code igniter... can the database class be modified in some way to detect a tinyint(1) column and automatically cast it to a bool and then just do this until MySQL supports a true boolean column? I know they're planning it, but...