CDbCriteria using with

have any one got an idea how to made CDbCriteria working with relations?

I have already write about this problem before month but nothing useful.


When you set Your query:

$criteria = new CDbCriteria;

$criteria->condition = " > 0 AND > 0  ";


Every time I have an error that there is no such column I cant find an answer for few weeks and this problem is a main problem in Yii wich I cant resolve.

I have try a lot of different ways with conditions with relations but nothing. If I write with(‘table2’) everything is ok, but I need to specify my data so I need to write the condition.

I try to set alias but this nothing helps. My query when she runs without condition with > 0 works perfect and selects values like:, table2.value1 etc.

But when I try to set this value in condition it throws an error.

Is there a bug of Yii with relations? I’m using 1.1.4 version.

Have somebody face this problem??

in your model table1 you have to setup

public function relations(){

return array(

'table2'=>array(self::HAS_MANY,'model2 name',''),



and use withouth conditions

$data = Mymodel::model()->with('table2')->findall();


I just before a second found the solution here:

Yii forum

There is very clearly described that Your condition must be described in a relation (where your relation is described) and than as You say, there’s no need to write the condition in your query because it is described in your relation.

Thanks for an answer, very glad for solving this one :)

thanks again ;)

I will help anytime if i can :)

You can set an alias for the with, explaining that the table should be loaded as table2, or you can use the default alias, t1.


$criteria = new CDbCriteria;

$criteria->condition = " > 0 AND > 0  ";


Hello Ignis

Could you please give again the link of the forum you told about?. this one is not working. I also need it!


for me works that link, check againне-понимаю-как-задать-у-таблицы-алиас-в-cdbcriteria/page__p__23512__hl__CDbCriteria+with#entry23512

To: zaccaria

Yes, I have try to use t1 alias and even set alias in a relation but it throws the same error because I have found Your answer somwhere in forums and You suggest to do this in the same way as in this topic.

I’ll try this one once again and if i’ll succed I will post about this.

Thanks again

Good luck


the link is working… but can smeone explain that in English?