Why oh why won’t CakePHP store my tinyint(1) data?

CakePHPOK, I actually know the answer to the question posed in the subject line. Despite the palpable suspense, I am sure 99.9% of my audience can tune out now. That leaves the remaining 0.001 readers to dive into this problem with me.

I’m doing a lot of CakePHP development these days, and I’m loving it. (What? The McDonald’s legal team is on its way over here? Damn. Fine, McDonalds. Have it your way. What, Burger King too? I guess I better make a run for the border. OK, joke’s over.)

Of course, plunging into a sea of someone else’s code is always fraught with a little peril, and today I found some. Here’s the scenario:

MySQL is a pretty cool database. I’m very loyal to it. But there are some things it should do, but it just doesn’t. One of those things is support a boolean data type. So, we make do. A common way to make do, and the way preferred by CakePHP, is to use a tinyint(1) field and just store 0 or 1 in it. In fact, CakePHP loves this approach so much that whenever it sees a data field that’s a tinyint(1), it “automagically” refuses to accept any values for that field other than 0 or 1.

That’s super-dee-duper. If that’s what you want. But I have a data table, my users table, as it happens, and in that table I used to have a field called admin, a boolean value. Either the user’s an admin or not. Great. But I decided to upgrade this to allow more access levels than just 0 or 1 (and while I was at it, changing the field name, shockingly, to access_level). I wanted to be able to support up to ten levels. Well, great! A tinyint(1) will nicely store values from 0 through 9, so I’m set!

Except… it didn’t work. Every time I tried to save a value greater than 1, I’d find that it had saved 1 as the value. I could change it to 0 just fine, but anything else became 1.

I checked the documentation and found ample evidence for the “automagic” behavior, so I figured there were a couple of possible changes I could make that would fix the problem: I could change it to a tinyint(2), or I could change it to a char(1). Since I decided I’d rather (in theory) allow letters in the field than double-digit numbers, I went with char(1).

Only it still didn’t work.

I did some more research, found that I seemed to be on the right track, but I was still confounded.

Then it occurred to me. I knew I had seen a cache directory somewhere in the labyrinth of directories and subdirectories and sub-sub-sub-sub-subdirectories in the CakePHP package. And I also knew that a framework as developed as CakePHP probably wouldn’t hit up the database for schema information constantly, so maybe… just maybe… it was cached.

I burrowed down in my application to the app/tmp/cache/models directory, and sure enough… there’s a cache file for each data table, with the schema in a serialized form. Well, it’s a cache, right? Nothin’ to lose. Trash can, here we come! I refreshed my page, and voilà! Success!

So… word to the wise (or, not so wise, like me… otherwise you’d probably already know):

  1. CakePHP will only store 0 or 1 in a tinyint(1) field. Period.
  2. If you change the schema for any of your data tables, and CakePHP acts like you didn’t… dump that cache!

We now return you to our regular, slightly less geeky programming.

Web browser usage stats. RPG-themed graphic. Geeks rejoice.

We web designers and developers need to keep track of more browsers than anyone should ever have to think about. (Isn’t one enough? And, even though I usually use Safari, can’t we all just agree on Firefox now and kill the rest?)

I’m just another in the chain of sites posting this graphic (which will probably proliferate exponentially now that it’s been on BuzzFeed): 123… and directly from here although this leaves me wondering who really is the originator. And of course, there’s the data source, cited in the image itself.

Well whoever came up with it, it’s pretty cool. And informative. I had forgotten the misfortune of Netscape 5, dying off two full years before Netscape 3 in the wake of the Mozilla split.

Enjoy…

Browser Wars

An open letter to Microsoft Word users (i.e. everyone)

Steve BallmerI am currently in the midst of developing a website for a client (great client, by the way), featuring a WYSIWYG text editor (TinyMCE) within its CMS. At some point along the way, I looked at the source code of some of the HTML that the client had inserted into the database via TinyMCE. With dismay, I noticed the likes of the following amongst the “actual” content:

<p><!–[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning /> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:BreakWrappedTables /> <w:SnapToGridInCell /> <w:WrapTextWithPunct /> <w:UseAsianBreakRules /> <w:DontGrowAutofit /> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]–><!–[if gte mso 9]><xml> <w:LatentStyles DefLockedState=”false” LatentStyleCount=”156″> </w:LatentStyles> </xml><![endif]–> <!–[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable {mso-style-name:”Table Normal”; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:””; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:”Times New Roman”; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} –> <!–[endif]–></p>

Ugh. For those keeping score at home, that would be more than a kilobyte of… well… absolutely nothing on a web page. Oh, except some browsers will turn it into a couple of extra, unwanted blank lines thanks to the paragraph tags wrapping it all.

The sad thing about all of this is that I am no longer even at a point where I look at that and say “What the hell is this?” I know exactly what the hell it is. It’s code generated by Microsoft Office for some inscrutable, presumably nefarious purpose. It seems to have no effect whatsoever on the presentation of the content on the page in any known browser or application that I’ve bothered to investigate. But whenever you copy-and-paste content out of a Microsoft Office application like Word and into a web-based text editor, or if you use Word’s “Save as Web Page” feature, the resulting HTML consists of significantly more of this bloat than of the content itself.

Aghast — or, at least, I would be aghast if I weren’t so numb to all of this now, after more than a decade of confronting it — I began crafting an email to the client, in my ongoing quest to reduce dependence upon Microsoft applications, one user at a time. But then I decided the client didn’t need my admonitions (although I don’t rule out the possibility that he’s reading this); the whole world does.

I was all set to copy and paste that draft email into this post. Unfortunately, although I had copied it to the clipboard, I didn’t paste it before going off and copy-pasting of the Microsoft garbage code above, and I also didn’t save it as a draft in Mail. Once again Microsoft seems to have the upper hand. Balllllmerrrrrrr!!!!!

The upshot of all of it, though, was simply that Microsoft Word generates copious quantities of garbage HTML and includes it in what gets put into the clipboard when you copy-paste content from a Word document into other applications (such as a WYSIWYG text editor box in a CMS). Most of the time there’s no visible effect from this garbage code (other than the fact that it increases page load times slightly by virtue of being more data to download), but there’s no way to know for sure that it’s not going to break a page in some browser, either now or at some point in the future when browsers adhere more strictly to XHTML DOCTYPE specifications. Plus, it’s just pointless garbage Microsoft is making me look at when I view source. I object to it on principle.

Then again, who am I to challenge the Goliath of software?

Why does this seem like a joke?

FruuppIt was bound to happen eventually, and now it has. Tonight I finally joined Twitter (follow me!). One of my first acts as a Twit (?) was to become a follower of some people I have already been following in more traditional fashions — if blogs and podcasts can be considered “traditional.” And those would be: gruber of Daring Fireball, and scottsimpson, lonelysandwich and hotdogsladies of You Look Nice Today.

I am already a little scared of the power of Twitter. It feels like I’m injecting other people’s thoughts directly into my brain, especially if you are foolish enough to delve into the public timeline. Truly frightening. And a stark reminder that, obscure though you may be, what you do on Twitter is public.

And now then, on to what I’m actually writing about. This tweet from scottsimpson brought to my attention (or to my recollection, as I think I had heard of them before) an obscure early ’70s Irish, as he puts it, “‘hengeprog” rock band named Fruupp.

The unofficial fan site seems suspiciously like an elaborately conceived parody: Spinal Tap, now with more Stonehenge. For a while I was convinced the band had never really existed, until I heard some rare live recordings (warning, it’s MySpace) and found their jaw-droppingly high-priced import CDs on Amazon. So it appears they are were real, after all! Intrigued, I am. (Sorry, been watchin’ a lot of Star Wars lately with the kids.) But not at those prices.

And now, as I drift off into dreamland, I ponder my ability to convey future thoughts within the constraints of 140 characters.