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.

So,

When you set Your query:





$criteria = new CDbCriteria;


$criteria->condition = " table2.id > 0 AND t.id > 0  ";


$criteria->with('table2');




Every time I have an error that there is no such column table2relation.id 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 table2.id > 0 works perfect and selects values like: table2.id, 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','table2.id'),

)

}

and use withouth conditions








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




Thanks,

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.

So:




$criteria = new CDbCriteria;


$criteria->condition = " t1.id > 0 AND t.id > 0  ";


$criteria->with('table2');




Hello Ignis

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

thanks!

for me works that link, check again

http://www.yiiframework.com/forum/index.php?/topic/4408-не-понимаю-как-задать-у-таблицы-алиас-в-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

thanks,

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