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

Update relational models

I have 3 related tables

Articles(id, title, ...)

Categories(id, name),

Articles_Categories(artilceId, categoryId)

When I do an update to the categories of one of my Articles, like this:


$article = Articles::findFirst();
//Adding new category e.g
$categories[] = Categories::findFirst();

$article->categories = $categories;
$articles->update()

the new associations are not added the the Articles_Categories

Here is my defined relationships:


class Articles extends Model{
    public function initialize(){
        $this->belongsTo("Redacteurs_id", 'AutoPoster\Backend\Models\Users', "id", array(
            'alias' => 'user'
        ));

        /*$this->hasMany("id", 'AutoPoster\Backend\Models\ArticlesHasCategories', "articleId", array(
            'alias' => 'categories'
        ));*/
        $this->hasManyToMany(
            'id',
            'AutoPoster\Backend\Models\ArticlesHasCategories',
            'articleId', 'categoryId',
            'AutoPoster\Backend\Models\ArticlesCategories',
            'id',
            array(
                'alias' => 'categories',
                'foreignKey' => array(
                    'action' => Model\Relation::ACTION_CASCADE
                )
            )
        );

        $this->skipAttributesOnCreate(array('dateAjout'));
    }
    ....


58.4k

HI I think problem in above.

    $categories[] = Categories::findFirst();

    $article->categories = $categories;


22.8k

How to fix it ?