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


35.6k

HI I think problem in above.

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

    $article->categories = $categories;


14.6k

How to fix it ?