BelongsToMany through with three join tables, can't get nested associations

Hello there,

running cakephp 3.8 I need help again with nested associations. The usecase is the following. I have items, I have tags, users can append multiple tags to an items. At the join table, the user_id who appended the tag is also stored and a really important piece of information. So when loading items, I need to get all tags for the items and all users who set the tag. Despite the “user-part”, the association works fine right now. When the user comes into play, I get several error messages (DB errors) pointing me, that I’m doing something wrong. So this is my current state:

joinTable structure
id | tag_id | foreign_key | section | user_id
data example:
1 | 10 | 20 | items | 2
2 | 6 | 11 | documents | 9
(So, the tag-system is shared over the application. Other “sections” as “items” can also be tagged).

ItemsTable

$this->belongsToMany('Tags', [
        //The name of the foreign key that references the current model found on the join table
        'foreignKey' => 'foreign_key',
        //The name of the foreign key that references the target model found on the join model, or list in case of composite foreign keys.
        'targetForeignKey' => 'tag_id',
        #'foreignKey' => 'foreign_key',
        'through' => 'TagsRelations',
    ])->setConditions(['section' => 'inboxitems']);

TagsTable

$this->belongsToMany('Items', [
        //The name of the foreign key that references the current model found on the join table
        'foreignKey' => 'tag_id',
        //The name of the foreign key that references the target model found on the join model, or list in case of composite foreign keys.
        'targetForeignKey' => 'foreign_key',
        'through' => 'TagsRelations'
    ]);

So the question is, where to declare the relation of who did set the tag to an item/tagrelation, that I can call the ORM from ItemsController or DocumentsController to get that information.
Right now, I just call from ItemsController $query->contain([“Tags”]). Do I have to work with TagsRelations instead of working with “Tags” and “Users”?

Thanks in advance.

Yes, you’d make sure you have a TagsRelationsTable, and it would have a belongsTo relation to Users.

Thank you. Meanwhile I figured out, but now I have the confirmation that this is the way to go. Thanks.