使用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. - 对不起,我的英语不好。