Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Many to many relation

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, articleid, categoryid)

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.



83.4k

These are the models you need, the first parameter of hasManyToMany is the local field in the model you're defining the relation:

<?php

namespace Calin\Core\Models;

class Articles extends Phalcon\Mvc\Model
{

    public function initialize()
    {
        $this->hasManyToMany(
            "id",
            "Calin\Core\Models\ArticlesCategories",
            "article_id",
            "category_id",
            "Calin\Core\Models\Category",
            "id",
            array('alias' => 'categories')
        );
    }
}
<?php

namespace Calin\Core\Models;

class ArticlesCategories
{

    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')
        );
    }
}
<?php

namespace Calin\Core\Models;

class Category extends Phalcon\Mvc\Model
{

    public function initialize()
    {
        $this->hasManyToMany(
            "id",
            "Calin\Core\Models\ArticlesCategories",
            "category_id",
            "article_id",
            "Calin\Core\Models\Article",
            "id",
            array('alias' => 'articles')
        );
    }
}


32.7k

Ok, thank you. And now if i want to get the categories for an article, should i call the alias ? $article->categories ? Or create a method that returns the related ?



83.4k
Accepted
answer

You can use any of them, magic methods:

$article->categories
$article->getCategories()

or define the method getCategories() in the model calling internally getRelated() (this if you have an IDE and need better autocompletion)



32.7k

Thank you very much !