Source order chaotic: Sort by name causing SQLSTATE errors

In one of the Magento theme I was working, when trying to sort by name in the category listing page, it throws to this:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'order clause'

Turns out I am not alone. A number of Magento users have reported similiar issue and a bug report is filed. But before I saw that bug report, I thought it must be something I messed up because Default theme shows no such symptom.

Though the problem doesn't affect entire site, nonetheless it needs to be fixed before I can introduce my Magento theme to the community.

Not wanting to pay for Varien support (economic is bad and I am carefully counting every dollar I spend :) ), I decided to get to the bottom of it by using the most time-consuming, a bit stupid but most effective way: so I installed another default theme on my local server, then move my custom phtml files to the new site's location. Granted, after working on Magento for almost a year, I do have specific suspects, the conclusion was either the catalog.xml or any list.phtml. A quick test turned out they are not trouble kids this time.

Skin folder couldn't cause problem that is for sure, so I decided to go with template files in the template/page folder. In this particular theme, Courtyard House Furnish, 2columns-left is used so is the only template that needs to go to the new site's location. Tada! This time I hit a jackpot: as soon as I did that, with a refresh cache routine, the sort by name breaks the site.

Culprit is caught! Now what?

Though the culprit is caught, still I needed to find the reason why it does what it does before I can solve it. As far as the markup concerns, the 2columns-left is clean and fine, but... something must have gone wrong with the right column where 'sort by' is located. Comparing the original template and mine, put aside the class/id names in the divs, the only differences is that I have a source order layout for SEO purpose; which, the leftcol is placed after the rightcol like so:

<div class="rightcol"> product listing and sort by toolbar</div> // floated right
<div class="leftcol">layered nav and other side blocks </div>// floated left

"This shouldn't be it, there is no way it will cause trouble", I thought, yet started moving the leftcol back to where it was, above the rightcol.

One more refresh cache routine, holding my breath, praying for miracle I clicked on sort by "name". Holy Magento! It doesn't break.

In Conclusion

My guess is, sort by name sorts in ascending order, and it circles through attributes in the layered nav first to filter for the 'name' attribute, then proceed to the product listing, so when layered nav is placed after the product listing (or toolbar), it gets confused. I also ran a test by keeping the above code order, and removed the layered nav entirely to confirm my assumption. The result affirms it.

Does this means no Source Order for Magento layout? Well, unless Varien considers this a bug and fixes it (I sure hope so), the options you have are:

  1. Use 2columns-right layout with leftcol, rightcol order, then have the leftcol floated right.
  2. Remove sortby name or don't use sortby at all.

Obviously the first option isn't SEO desirable if you have a long list of layered nav.