CakePHP Expandable Behaviour Tip

When YYZtech was being updated to use the VSS (Venue Storage System) from the previous system, it became clear that VSS needed to be updated as well. One of the original goals of VSS was that it could be a directory system for different kinds of things as long as they where physical venues of some kind and so they had certain properties like an actual address, hours they open to the public, etc.

As part of that, a way of storing “meta” information was needed, that is information that was unique to a venue or type of venue that not all would have, much like WordPress lets additional information about a post be stored in a separate table which makes, say, adding an ISBN number field to book reviews easy.

After rolling our own for several projects, Felix Geisendörfer’s Expandable behaviour was chosen because it was simple to set-up and use and it just worked. Hopefully CakePHP at some point includes a similar functionality as an “official” add on much like virtual fields was added in CakePHP 1.3

Which leads to a “gotcha”….
One issue combining virtual fields and the Expandable behaviour is that virtual fields can end up being interpreted as new fields by Expandable and Expandable then saving them. What’s worse, is that when the table entry is loaded, the virtual field and field from Expandable are combined with one overwriting the other.

This particular behaviour happened when we added a way of cloning venues, removing and replacing certain information along the way. The reason for this would be adding a chain store location; the address and phone number would change but the description or products sold probably wouldn’t. Now the table that stored the venues had a virtual field, full_name, that contained the name and sub-name of a venue (e.g. “Joe’s Bar”, “and Delicatessen” as “Joe’s Bar and Delicatessen”) which Expandable saw as a new field and saved the virtual field to its table and led to about 20 minutes trying to figure out if there was some kind of weird caching issue going on.

Anyways, the fix was to turn off the virtual fields like so:
$this->Venue->virtualFields = false;

…and that was the end of that problem.