CDbCriteria with relations?

Hi,

can I use relations building my query with CDbCriteria like this:



$criteria->condition = "myRelation.name = :name";


$critera->params = array(":name"=>'John');


?

This should be fine. You just need to make sure the table alias is correct.

Any luck getting this to work?

I'm trying the same thing with a many-to-many relation and I'm not having any luck.

My relation is defined as such:

  'team'=>array(self::MANY_MANY, 'Team', 'TeamPlayer(teamId, playerId)'),

And my criteria is as follows:

  $criteria = new CDbCriteria();

  $criteria->condition = 'team.someCondition=:someCondition';

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

When I execute the query, I get this:

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'team.someCondition' in 'where clause'

Thoughts?  Thanks in advance!

You can turn on logging and see what's the generated SQL query, to see where the problem lies. I suspect a table alias naming issue…

Indeed, it was a table aliasing issue.  I needed to define the alias name in my relation.

See this post for details…

http://www.yiiframew…pic,2606.0.html

Thanks again!