Associating _joinData

So i have 2 tables with both having association belongsToMany to each other. The join table between these 2 has some additional fields and one of them is foreign key to third table. My question is how can i load this association of _jointData between these 2 tables?

Example:

Table Articles belongs to many Tags and Tags belongs to many Articles. Join table for both is ArticlesTags which has columns: article_id, tag_id and additional field image_id which is connected with Images table. When in controller, how can I conatin() so that _joinData in view contains not just image_id but the Image object as well?

just use normal contain e.g.

Articles BelongsToMany Tags
Tags BelongsToMany Articles.

just use Articles->find()->contain([‘Tags’]);

and to get join data

$data->tags->_joinData->data_from_join_data

I’m afraid you misunderstood my question… if i do like you said I’m gonna have _joinData and be able to access it like you said but i will only have accessible image_id and not image entity with all data. Your line Articles->find()->contain([‘Tags’]) will give me array of Tag entities for that article but not the image entity from _joinData which represents repository ArticlesTags.

how many table you trying to join??

where you planing to place the image??

51

have you try to use

$data->Articles->find()->contain([‘Tags’, ‘Images’])??

that how you access it. From Articles, Tags, Images, or directly from ArticlesTags.

example :
from PostsController that not related with image

$this->loadModel('Images'); or
$this->loadModel('ArticlesTags');

$this->Images->find(); or 
$this->ArticlesTags->find()->contain('Images');

No it doesnt work that way… this would only contain images if articles had image_id field

now you requirement is different from your qestion.

so you want the article to load the image event it doesn’t have image, what image it going to load if it doesn’t have an image??

Hmm let me try to reformulate the question: Imagine that articles have images like you said, but also imagine that this jointable ArticlesTags has Images as well… which are not the same. I need the image object from ArticlesTags table… not the Articles table? Check the screenshot above… i need the images from middle table, not the left one. (they are not the same)

owhh… i see you the image object loaded inside _joinData, right?

to what i know _joinData will only contain the data of joinTable, article_id, tag_id, image_id. with some extra field to keep the data inside the joinTable it self or if you need to use join columns outside of the conventions, e.g (HasMany Through)

documentation to refer to
https://book.cakephp.org/3.0/en/orm/associations.html#using-the-through-option

so the answer is, you cant do that. IMHO

it should be possible using through option as @donnidani said but you would have to alias table

$this->belongsToMany('ArticlesTagsImages', [
     'classname' => 'Images', 
     'through' => 'ArticlesTags'
]);

and then you could

$this->Articles->find()
->contain([
   'ArticlesTagsImages',
   'Tags'
]);

or you could create another table/model and have it belongsTo at those 3 tables (images, tags, articles) and then call it

$this->Articles->find()
->contain([
    'ArticlesTags' => [
        'Image',
        'Tag'
]);

Ok fixed it with help of this post: Trying to get additional data using BelongsToMany with multiple tables

Thx everyone for help