CDbcriteria error without using alias

This is the working code:


        	$criteria = new CDbCriteria;

        	$criteria->condition = 'user_id=:currentUserID';

        	$criteria->params = array(':currentUserID'=>$userID);

        	$criteria->alias='r';

        	$criteria->select = 'item.title, r.item_quantity';

        	$criteria->join = 'JOIN item ON item.id=r.item_id';

        	$dataProvider = new CActiveDataProvider('RecuringOrder',array('criteria'=>$criteria));

		$this->render('index', array('dataProvider'=>$dataProvider));

This code doesn’t work:




        	$criteria = new CDbCriteria;

        	$criteria->condition = 'user_id=:currentUserID';

        	$criteria->params = array(':currentUserID'=>$userID);

        	//$criteria->alias='r';

        	$criteria->select = 'item.title, recuring_order.item_quantity'; // replace alias r with recuring_order, the real table name in mySQL

        	$criteria->join = 'JOIN item ON item.id=recuring_order.item_id'; // replace alias r with recuring_order, the real table name in mySQL

        	$dataProvider = new CActiveDataProvider('RecuringOrder',array('criteria'=>$criteria));

		$this->render('index', array('dataProvider'=>$dataProvider));

CDbcriteria is the most confusing part for me. Why I have to use alias in the above situation?

Thank you very much!

The implicit primary table alias ‘t’ should work.

See this section of the guide.

/Tommy

yes, the implicit t works without alias definition. I just read the guide that you suggested.

Do you think I can make the following conclusion?

If there is same column name in two tables in a query, the primary table has to be an alias, either default "t’, or other alias, e.g. r, that you assign.

Thank you!

That’s my understanding.

For columns of related tables you can use the relationship name or an alias of your choice.

/Tommy