Please help me with CActiveRecord Join Table

Hi, i have a question about joining table. I have a case where i need to display value from three related table.

table merchant : merchant_id

table coupon : coupon_id,merchant_id

table log : log_id, coupon_id

I need to query like this :

select b.* from tbl_coupon a, tbl_coupon_log b, tbl_merchant c

where b.coupon_id = a.coupon_id and a.merchant_id = c.merchant_id

How to do this using relations and CActiveRecord? Or any better idea? Thanks.


If you have a class called Coupon and you search like:

Coupon::model()->with("log")->findAll('merchant_id=:id', array(':id' => $id));

Then in your Coupon model you specify within the relations function:

'log' => array(self::HAS_MANY, "Log", "coupon_id")

Provided your Coupon id field is the only primary key in that table.

That will perform eager loading. To not perform eager loading you can just take out the with() in the first query and do:


That should hopefully do the trick.


Ok so you have totally changed the question.

Please say that you have changed your question or post a new reply when you do.

If you look at the link I sent you can see that relations offer more options, also with() offers options with which to create quite nice queries.

Yes, im sorry,im still new in this. I’ll inform next time when i change my question.

Thanks so much for the reply Sammaye , it helps me a lot! =)