Yii Alias Bug

Hi,

This is very annoying alias problem i am facing since i started working on Yii. Add created a topic in Discussions but could not find any solution.

This is my controller code, i have this kind of code in many controllers,


$criteria = new CDbCriteria(array(

                        'condition' => 'u.firstname LIKE "%' . $_GET['term'] . '%"',

                        'select' => 'u.firstname, u.user_id',

                        'join' => 'INNER JOIN users_department ud ON ud.user_id = u.user_id',

                        'group' => 'u.user_id',

                        'alias' => 'u'

                    ));

            $modelUsers = Users::model()->findAll($criteria);

This is my default scope in Users Model


public function defaultScope() {        


        return array(

            'condition' => $this->getTableAlias(FALSE, FALSE) . '.active="Y"',

        );

    }



This result me with an mysql error t.active is an unknown column.

if i remove ‘$this->getTableAlias(FALSE, FALSE)’ from default scope it will show me ambiguous column mysql error for user_id.

Even if i specified ‘u’ as the alias in CDbCriteria, in model alias is ‘t’.

$this->getTableAlias(FALSE, FALSE) function doens not return alias from CDbCriteria instead it defaults to ‘t’. WHY ?

Regards,

Praveen J.

Make sure of the SQL query you would execute in PHPmyadmin or Chive.

Then enable Yii logging and YII_DEBUG.

Then look at the Yii log and make sure the SQL query generated by CdbCriteria is the same.

Hi saegeek,

Sorry i dint get what u were trying to say. The problem is with Yii’s $this->getTableAlias(FALSE, FALSE) this does not get u the alias specified in CdbCriteria for the default scope instead it takes ‘t’ as the alias.

Regards,

Praveen J

Hi Praveen,

It seems like when you’re performing relational queries using AR the main table always get ‘t’ as alias.

You can find more details here.

Regards,

Adiel Cristo

Hi Adiel Cristo,

Thanks a lot, i will be changing all the primary tables alias to ‘t’. There are lot of relational queries in my project as it is almost at the finishing stage, thought there would be a simple solution. But think this is the only way to come across this problem. So will be doing that only… :)

Regards,

Praveen J