使用Yii框架。yii活動記錄加入模型
我有3個型號。
文章 - 表項目(編號,名稱)
ArticlesToAuthors - 表articles_to_authors(ID,article_id的,AUTHOR_ID,類型)
作者 - 表格作者(ID,姓名)
我需要獲取作者。*,article_to_authors.type爲特定的article_id。
我試圖讓在ArticlesToAuthors模型這樣的關係:
'authors' => array(self::HAS_MANY, 'Authors', array('id' => 'author_id'),),
然後我做了查詢:
$authors = ArticlesToAuthors::model()->with('authors')->findAll(array('condition' => 'article_id=2217'));
foreach($authors as $a)
{
//this is not working
#var_dump($a->authors->name);
#var_dump($a->name);
//this works fine
foreach($a->authors as $aa)
{
var_dump($aa->name);
}
}
我能得到的所有活動記錄對象的一個,而不是在foreach做foreach? 我需要一個模擬到SQL 「的選擇ATOA ,一個 FROM
articles_to_authors
ATOA LEFT JOIN作者一個ON atoa.author_id = a.id WHERE atoa.article_id =:。article_id的」我這樣做對嗎?
p.s. - 對不起,我的英語不好。