DB order clause problem

Hi all,

I am having the following issue with my blog tutorial:

In my relations() method in the Post model I have the following relation declaration (copied directly from the tutorial):




return array(

            'author'=>array(self::BELONGS_TO, 'User', 'authorId'),

            'comments'=>array(self::HAS_MANY, 'Comment', 'postId',

            'order'=>'comment.createTime'),

            'tagFilter'=>array(self::MANY_MANY, 'Tag', 'PostTag(postId, tagId)',

                'together'=>true,

                'joinType'=>'INNER JOIN',

                'condition'=>'??.name=:tag',

            ),


		);



Yii does not recognize this part:


'order'=>'comment.createTime'),

and throws the following error:

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘comment.createTime’ in ‘order clause’

I can confirm that I have table name comment and there is createTime field in that table. However, if I change that line with the following it seems to work:


'order'=>'??.createTime'),

I saw these question marks used in another topic in this forum (not related to this problem) so decided to test it. Can someone explain how exactly Yii understands the question marks and how they should be used correctly?

I have no idea and hope that someone can help me understand why this is happening. Here is the rest of the error page:

Have look here:

http://www.yiiframework.com/doc/guide/database.arr

Go to the section "Disambiguating Column Names" halfway down the page.

/Tommy

From what I saw


'order'=>'comment.createTime'),

should work like this


'order'=>'comments.createTime'),

According to: http://www.yiiframework.com/doc/guide/database.arr#disambiguating-column-names I tried to use an order for my data retrieval.

At first, I used the following code, leading to an error:




public function getSubCategoriesOptions2($parentID=null) {

if(!is_null($parentID)) {

 $criteria=array(

 'condition'=>'categoryID='.$parentID,

 //'order'=>'SubCategories.name ASC',

);                     

$values = $this->with('category')->findAll($criteria);

return CHTML::listData($values, 'id', 'name');

} else {

 $criteria=array(

 'order'=>'SubCategories.name',); // <-- HERE IS THE PROBLEM

 $values = $this->with('category')->findAll($criteria);

 return CHTML::listData($values, 'id', 'name','category.name');}

}



The SQL Statement, Yii constructed from the input above, is:

[sql]

SELECT

&quot;SubCategories&quot;.&quot;id&quot; AS &quot;t0_c0&quot;, 


&quot;SubCategories&quot;.&quot;categoryID&quot; AS &quot;t0_c1&quot;, 


&quot;SubCategories&quot;.&quot;name_en&quot; AS &quot;t0_c2&quot;, 


&quot;SubCategories&quot;.&quot;name_de&quot; AS &quot;t0_c3&quot;, 


&quot;SubCategories&quot;.&quot;name&quot; AS &quot;t0_c4&quot;, 


t1.&quot;id&quot; AS &quot;t1_c0&quot;, 


t1.&quot;name_de&quot; AS &quot;t1_c1&quot;, 


t1.&quot;name_en&quot; AS &quot;t1_c2&quot;, 


t1.&quot;name&quot; AS &quot;t1_c3&quot; 

FROM

&quot;SubCategories&quot; 

LEFT OUTER JOIN

&quot;Categories&quot; t1 ON (&quot;SubCategories&quot;.&quot;categoryID&quot;=t1.&quot;id&quot;) 

ORDER BY SubCategories.name

[/sql]

The problem is, that at least "SubCategories" has to be in quotation marks, to get the sql query to work. It should be ORDER BY "SubCategories".name.

Providing this information in my PHP code, the query works:


        

public function getSubCategoriesOptions2($parentID=null)

	{

		if(!is_null($parentID)) {

                    $criteria=array(

                            'condition'=>'categoryID='.$parentID,

                            //'order'=>'SubCategories.name ASC',

                    );

                    $values = $this->with('category')->findAll($criteria);

                    return CHTML::listData($values, 'id', 'name');


                } else {

                   $criteria=array(

                        'order'=>'"SubCategories".name',// <-- NOW IT WORKS

                   );

                   $values = $this->with('category')->findAll($criteria);

                   return CHTML::listData($values, 'id', 'name','category.name');

                }

	}



If I want to order with more than one column, I have to use t1…tn instead of the real table names. For example all the following lines of source code




'order'=>'category.name, "SubCategories".name'

'order'=>'"category".name, "SubCategories".name'

'order'=>'"Categories".name, "SubCategories".name'



lead to an error.

The only way i was able to get it working is using




'order'=>'t1.name, "SubCategories".name'



I guess, that this should not be the expected behavior of Yii?

Greetz,

Bergtroll