CakePHP/MySQL: Want a Boolean? Use TINYINT(1), not BIT(1)

I was recently creating some new MySQL tables that stored booleans, so I thought I would do things (what I presumed to be) properly and make them BIT(1) columns, rather than TINYINT(1). However, when I came to try to save values to this field using CakePHP, everything was saved as 1, even if the value in the data array was 0, false, “”, “0” or anything else ‘zero-ey’ or ‘falsey’.

It turns out that BIT fields are not supported by Cake, and so you should just use TINYINT(1) instead!

Furthermore, Cake will assume a TINYINT(1) field is intended as a boolean field, and will only allow you to assign it a value of 0 or 1, even though TINYINT(1) can store values from -128 to 127 (or 0 to 255 unsigned). If you try to save any value other than 0 or 1 to this field, it will be saved as 1 (but note that false, “” and an empty array will cause a save error).

Stringify an Object in Javascript

I just Googled (other search engines are available) variations of “javascript convert object to JSON”, assuming that it would be something that is completely straightforward and easy to do, either with basic Javascript or using JQuery, which we have loaded in most of our applications anyway. It turns out basic Javascript can’t do it, and nor can JQuery (although Jquery does have the parseJSON function for parsing a JSON string – jQuery.parseJSON( json )). Thanfully, there is a simple way to do it, but it’s not quite as simple as I’d hoped, if you want to fully support all browsers.

The Simple Way (won’t work in IE7 or below))

JSON.stringify(yourObject);

All ‘modern’ browsers (see exactly which ones here: http://caniuse.com/json, but basically it’s everything except IE7 and below) natively support the JSON object, which allows you use JSON.stringify(object) to convert and object to a JSON string, and JSON.parse(string) to go back the other way. So if you’re not bothered about supporting old versions of IE, you can do it this way.

The marginally more complicated way (for full cross-browser compatibility)

json2.js (available here: https://github.com/douglascrockford/JSON-js) gives you the same stringify and parse functions as above, and is pretty small when minified. It seems to be the best option out there if you want to support the poor souls forced to (I really hoping no-one is choosing to) use IE7 or below.

Eclipse: Adding File Associations

We regularly write PHP, using the CakePHP framework, in Eclipse (Juno, with PHP Development Tools (PDT)). The view template files in CakePHP have the .ctp extension, which Eclipse does not, by default, recognise as PHP files, so you do not benefit from any of the helpful Eclipse PHP tools. However, you can add an association (i.e. tell Eclipse that .ctp files are PHP files), as follows:

  1. Go to Window > Preferences to open the Preferences box
  2. Go to General > Content Types in the menu
  3. In the Content types box, expand Text and select “PHP Content Type”
  4. Click “Add”
  5. Type “.ctp” in the box that appears and click OK
  6. Click OK to close the Preferences box

Note that you will have to close and then reopen any .ctp files that are already open in order for them to be recognised as PHP files.

Eclipse: Changing Project Type

I’ve just added a new project from an existing location in Eclipse Juno, and for some reason it wouldn’t let me create it as a PHP project. Instead, I had to just create it as a general project. Having done this, it is fairly easy to convert it to a PHP Project, by doing the following:

  1. Open the .project file for the new (non-PHP) project in a text editor
  2. Open a .project file for an existing PHP project (or, if you haven’t got an existing PHP project, create one in your workspace and use the .project file from that)
  3. Copy the <buildSpec> and <natures> sections from the PHP .project file (from 2. above) to the .project file for the project that you want to change to a PHP project (from 1. above)
  4. Save the new modified .project file and refresh the workspace
  5. The general project should now be a PHP project

The .project file for my PHP projects looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
   <name>project_name</name>
   <comment></comment>
   <projects>
   </projects>
   <buildSpec>
      <buildCommand>
         <name>org.eclipse.wst.validation.validationbuilder</name>
         <arguments>
         </arguments>
      </buildCommand>
      <buildCommand>
         <name>org.eclipse.dltk.core.scriptbuilder</name>
         <arguments>
         </arguments>
      </buildCommand>
   </buildSpec>
   <natures>
      <nature>org.eclipse.php.core.PHPNature</nature>
   </natures>
</projectDescription>

Magnolia CMS: Getting node/page/paragraph IDs and user details using JSP

Just a quick note to remind me, and let anyone else who tries to do this know, how to access the unique id (uuid) of a page or paragraph, and details about the current user in a JSP page in Magnolia CMS. We are watching to save, via AJAX, information about how students use pages and paragraphs (in particular self-test question paragraphs) within our Magnolia installation, and need these details in order to do this.

Displaying the node/page/paragraph ID

If you put this code in a page template, it will show you the unique ID for the page. If you put it in a paragraph template it will show you the unique id for that paragraph. These IDs do not change when you move a page, or a paragraph within a page.

<cms:out nodeDataName="uuid" var="uuid" />
<p>${uuid}</p>

Displaying the name of the current user

<jsp:scriptlet>
   String user = (info.magnolia.context.MgnlContext.getUser().getName()).toString();
   out.println("&lt;p&gt;" + user + "&lt;/p&gt;");
</jsp:scriptlet>

Maths with QTI – Explanation of Help Guide for Maths Entry Questions

Here’s the link to the Help Guide for Maths Entry Questions, if you’ve come here looking for that. This post explains why we’ve created that guide.

We’re starting to deliver Maths questions online using QTIWorks, and are putting together a help guide for users, as it is not always easy to know how to correctly represent mathematical expressions in a maths answer box.

The “Input Hints” provided in QTIWorks are of some help, but are perhaps not the most user friendly and could be improved by the addition of examples, and perhaps some reordering to place the simpler and more commonly used inputs nearer the top of the list.

QTIWorks Input Hints
QTIWorks Input Hints (click to view full size)

To supplement (/replace) this hints box, I’ve started to write a (hopefully) simple guide to using the maths input box, with some examples. This is a work in progress, so will evolve, but hopefully is a reasonable starting point. Any comments are very welcome.