I'm trying to save additional data to join table in CakePHP 3. Association looks like: articles => model_tags <=> tags (tables).
In TagsTable and Articles Table I've got:
$this->belongsToMany('Tags', [
'joinTable' => 'model_tags',
'foreignKey' => 'foreign_id'
]);
Associations between articles and tags are saved correctly in model_tags table. But I would like to save additional data to field 'model_name' in model_tags table, because in the future I would like to use model_tags table to another models. So in that case, all tags associations saved in model_tags through articles should have in field 'model_name' string "articles".
The main point is - where should I put my code? I'm fighting with this problem since few hours. I think that beforeSave should be the best option.
Already I figured out, that I can put data in separate record:
$tag = $this->Tags->findByTitle('heregoestagtitle')->first();
$tag->_joinData = $this->ModelTags->newEntity();
$tag->_joinData->model_name = 'articles';
$this->Tags->link($entity, [$tag]);
But of course in my case it won't work, because I will save few tags per article, so there will be also few association records in model_tags table.
How can I put data in join table in correct way?