Using Yii framework.
I have 3 models.
Articles – table articles(id, name)
ArticlesToAuthors – table articles_to_authors (id, article_id, author_id, type)
Authors – table authors (id, name)
I need to get authors.*, article_to_authors.type for specific article_id.
I was trying to make relation in ArticlesToAuthors model like that:
'authors' => array(self::HAS_MANY, 'Authors', array('id' => 'author_id'), ),
then i made query:
$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);
}
}
-
Can i get all active record object in one and not to do foreach in foreach?
I need an analogue to sql “SELECT atoa., a. FROMarticles_to_authorsatoa LEFT JOIN authors a ON atoa.author_id=a.id WHERE atoa.article_id=:article_id” -
Am i doing right?
p.s. – sorry for my bad english.
It looks like you need the following relations:
in your
ArticlesToAuthorstable:and, for completeness, in your
Authorstable:This should allow you to access
$a->author->name. Not tested, that’s just off the top of my head.