We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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, 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.



98.9k

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


51.1k

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 ?



98.9k
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)



51.1k

Thank you very much !