I am trying to perform relational query to the database and in where clause I have one ambiguous column, so I am using table.ambiguouscolumn_name in where clause, but its giving me error like table.ambiguouscolumn_name is not defined.
For example
$usermodel = new user;
$criteria = new CDbCriteria;
$criteria->select = array("UserId", "FullName","CreatedDate","Status");
$criteria->condition = "user.CustomerId=:CustomerId";
$parameters[":CustomerId"] = $this->customerid;
$pages=new CPagination($usermodel->count($criteria));
Exception I am getting is -
I went through the debug logs, its generating query like -
Query says its creating alias for the table as t but in my where clause its user.CustomerId, so its giving exception.
I hope there must be some simple solution to this.
I have user table and group table, both having common CustomerId field. I want to retrive some data from group table to which user is associated with and user must belongs to particular CustomerId.
If the query is like
How Yii will interpret this query? Is it going to create alias t for both or t1 for user and t2 for group? And what should I use in where clause.
I think its bit confusing to blindly use alias in where clause and suppose in next version yii changes this alias concept to something else?
The solution to this is use t instead of user ( tablename ) in where clause. I think this shouldn’t be only solution to this problem. It is very bad to go to each and every page and change the where clause?
Sorry to revive an old thread but I just wanted to post this in case someone is having the same problem as me (related to the thread, of course).
This is the situation: I have CGridView showing columns from another table and everything worked fine (I followed this tutorial: http://www.yiiframework.com/forum/index.php/topic/17595-column-disambiguation-on-named-scopes/), except that when I use the filter row and enter a value for column "name" of the original table, I got an SQL error about ambiguity (had to disambiguate the columns) because there was a column "name" in the 2nd related table.
So, to fix this, I went to my original table’s model file (/protected/models/OriginalTable.php) and modified the criteria within the search() function, like so:
From:
$criteria->compare('name',$this->name,true);
To:
$criteria->compare('t.name',$this->name,true); #notice the added "t." before the column name
This fixed the ambiguity issue. I hope it helps the next guy or girl