‘Length’ attribute or column name in database can clash with jQuery and javascript
In what was one of the worst bugs I have come across in a while, mainly my fault, I learned the hard way that using the name ‘length’ for an object attribute may be a bad idea. Along the same lines as using ‘window’ as an attribute name. These things tend to cause clashes in framework and javascript libraries.
So more specifically with the yii framework, which uses jQuery to handle gridview filtering, I debugged the hell out of other people’s JavaScript code until I learned from line 59 on the jQuery core library that the each method on a jQuery object uses the length property of a jQuery and javascript object to iterate over its properties.
It goes as follows. Say I have a ‘windowdrape’ table and object. This has a length and width, in inches, of how big the drape is. So I make a ‘length’ column. The yii framework hands my model object off to javascript. So now in javascript I have a windowdrape object. So WindowDrape.length to jQuery is how many attributes the WindowDrape has. I expect it to be how long the window is in inches. So I set it to 98, and now all of a sudden jQuery will iterate the thing 98 times, and the attributes on WindowDrape are all messed up because of the bogus length attribute.
The lesson here, especially if you use the yii framework is Don’t name a column or model object attribute ‘length’. These things are also why I avoid using ‘name’ or ‘window’ etc. for attribute names.
