Yii Framework Forum

Ambiguous column in relation's order clause

(Jeff) #1

I am encountering an error with the order clause of a relation. Following are examples that are similar to the code I am working with (but not the actual code for the sake of privacy):

class Example extends CActiveRecord
    public function relations()
        return array(
            'relatedObjects' => array(self::HAS_MANY, 'RelatedObject', related_object_id', 'order'=>'my_order ASC'),

In this case, my_order is referring the a column in the related_object table. The RelatedObject class is not the only class with my_order, so it can be ambiguous. I’ve tried using t.my_order, among other things, but then I get an Unknown column error instead.
Does anybody know how to make this work?

(Mehdi Achour) #2

To what table does the my_order field you’re trying to order with belongs?
Could you show how you wrote that and what was the full error message?

From the docs:

In relational AR query, the alias name for the primary table is fixed as t , while the alias name for a relational table is the same as the corresponding relation name by default.

(Alirz23) #3

try full table name instead of alias table_name.my_order

(Jeff) #4

What ended up fixing this was using the relation name.

This worked

‘order’=>‘relatedObjects.my_order ASC’