Table Alias Issue

Hi My Default Scope for Subject Model Would Be,

public function defaultScope() {

    return array(


        'condition' => $this->getTableAlias(FALSE, FALSE).'.school_id = "' . Yii::app()->session->get('school_id'),


    );


} 

Controller Function Would Be,

$subjects = new CDbCriteria(array(

                    'condition' => 's.active="Y"',


                    'join' => 'inner join users u on u.user_id = s.created_by'   


                    'alias' => 's'


                ));

$modelSubject = Subjects::model()->findAll($subjects);

Both Subjects and Users as school_id field, hence if i dint specify $this->getTableAlias(FALSE, FALSE) in default scope condition it would result in ambiguous column error.

But as of now its showing me t.school_id is unknown column, because $this->getTableAlias(FALSE, FALSE) return ‘t’ as alias not ‘s’.

How to overcome this issue by getting table alias as ‘s’ as specifed in CDBCriteria using $this->getTableAlias(FALSE, FALSE) in default Scope.

Thanks You,

Praveen J

Hi Praveen,

Even i am facing the similar issue of table alias > The function getTableAlias(FALSE, FALSE) doesnot give me the table alias which i specified in Cdbcriteria but it defaults to "t".Let me know if you have found any solution.

Thanks

Sudheer

Hey, Try this:




$modelSubject = Subjects::model();


$alias = $modelSubject->getTableAlias(false, false);


$subjects = new CDbCriteria(array(

                        'condition' => "$alias.active='Y'",

                        'join' => "INNER JOIN users u on u.user_id = $alias.created_by'"

                    ));


$modelSubject = $modelSubject->findAll($subjects);



Hi VINAY Kr SHARMA,

Thanks a lot. Very Good suggestion.

Regards

Praveen J.