Yii並不會默認進行關聯查詢,我們需要使用類似如下的代碼:
Article::model()->with('Category')->together()->find...
這個語句將按照在Model中定義過的Relations來建立關聯查詢語句,默認LEFT JOIN。如果不使用together,只使用with方法,那麼生成的是子查詢:
select ... from articles where id in (select id from article_categories)
如果不使用with,那麼等同於未定義關聯關係的單表查詢。