CGridView sorting v. defaultScope()

In a model defaultScope(), I have ‘order’= >‘someColumn’,…

The someColumn column is displayed in a CGridView.

If the user clicks on that column heading, it generates invalid SQL, because it’s trying to sort by a column that’s already in the defaultScope(). The column name is added to the ORDER BY clause twice.

The problem seems to be in CDbCriteria.MergeWith(). It merges the Order properties of the current criteria object, blindly with the new ones. Nothing is done to prevent column duplication.

What am I missing? This seems like it should be a very common problem; yet I can find nothing in the forums.

This bug was entered and replied to:

http://code.google.com/p/yii/issues/detail?id=2539

Reply from Qiang:

“While there is a fix for this, it is ugly and inconsistent. Checking how CDbCriteria merges other parts, such as ‘join’, you will find similar problem may occur. This is the limit that defaultScope brings. I hope we can find a nicer and more systematic solution in 2.0.”