Hello,
I have a small issue with many to many models. The documentation is not very clear about this (or not for me). If i have 3 tables (and the models):
Articles (id, title) Categories (id, name) ArticlesCategories (id, article_id, category_id)
Which is the best way, to emulate this relation in models ? If i will show a list of articles, i would like to show the article categories - something like:
$article->getTitle() | implode(',', $article->getCategories())
What I have until now:
Article model:
public function initialize()
{
$this->hasManyToMany(
"category",
"Calin\Core\Models\ArticlesCategories",
"article_id",
"category_id",
"Calin\Core\Models\Category",
"id",
array('alias' => 'Categories')
);
}
public function getCategories($parameters = null)
{
return $this->getRelated('Categories', $parameters);
}
Category model:
public function initialize()
{
$this->hasManyToMany(
"id",
"Calin\Core\Models\ArticlesCategories",
"category_id",
"article_id",
"Calin\Core\Models\Article",
"id",
array('alias' => 'Articles')
);
}
ArticlesCategories model:
public function initialize()
{
$this->belongsTo('category_id', 'Calin\Core\Models\Category', 'id', array('alias' => 'Category'));
$this->belongsTo('article_id', 'Calin\Core\Models\Article', 'id', array('alias' => 'Article'));
}
Any help would be appreciated. Thanks.