Hello, I have a few questions about Join in Cakephp

Hello, Thank you for reading it.
I’m studying cakephp and have a few questions about Join in Cakephp.
I coudn’t find any appropriate answer on internet

1, What is difference between innerjoin and innerjoinwith (like leftjoin and leftjoinwith) I thought It is related with performence issue, but couldn’t find any clue.

2, I can’t get the exact difference between “matching” and "contain"
I know the way they retrieving data is different, but I thought the result looks same as the result of matching and matching is using innerjoin, and contain is using leftjoin.

but but I can’t find what is the difference between the code below(using contain) and matching.

$query = $articles->find()->contain([
‘Comments’ => function ($q) {
return $q
->select([‘body’, ‘author_id’])
->where([‘Comments.approved’ => true]);
}
]);

3, what is “_matchingData” in matching?
I read this description, that is
"The innerJoinWith() method works the same as matching(), that means that you can use dot notation to join deeply nested associations: Again, the only difference is that no additional columns will be added to the result set, and no _matchingData property will be set."

but I couldn’t find any description about _matchingData. even in API… what it is?

you can response one of them, it is okay. please help me out

1, What is difference between innerjoin and innerjoinwith (like leftjoin and leftjoinwith) I thought It is related with performence issue, but couldn’t find any clue.

=>
(1) here ‘innerJoinWith’ is a callable which will apply right/inner join
(2) for left join ‘leftJoinWith’ is the callable

https://book.cakephp.org/3.0/en/orm/query-builder.html#using-innerjoinwith
https://book.cakephp.org/3.0/en/orm/query-builder.html#using-leftjoinwith

2, I can’t get the exact difference between “matching” and "contain"
I know the way they retrieving data is different, but I thought the result looks same as the result of matching and matching is using innerjoin, and contain is using leftjoin.

but but I can’t find what is the difference between the code below(using contain) and matching.

$query = $articles->find()->contain([
‘Comments’ => function ($q) {
return $q
->select([‘body’, ‘author_id’])
->where([‘Comments.approved’ => true]);
}
]);

=> In short contain applies leftJoin and matching applies right/inner join to the query.
please read this first line on this link https://book.cakephp.org/3.0/en/orm/query-builder.html#using-innerjoinwith

3, what is “_matchingData” in matching?
I read this description, that is
"The innerJoinWith() method works the same as matching(), that means that you can use dot notation to join deeply nested associations: Again, the only difference is that no additional columns will be added to the result set, and no _matchingData property will be set."

but I couldn’t find any description about _matchingData. even in API… what it is?

=> _matchingData() is an entity property created when matching() is used. It contains data which is selected matching

$query = $articles->find(); 
$query = $query->matching('Comments', function($q){
			$q->select(['Comments.body', 'Comments.author_id']);
			$q->where(['Comments.is_approved' => 1]);
		return $q;
});

Above query will have _matchingData property contained with Comments.body, Comments.author_id
1 Like

Thank you for answering to my questions.
You saved me!
I just want to check if “callable” which you commented at question number 1 is same as the link below.

http://php.net/manual/en/language.types.callable.php

Thank you for your answer again!

Yes callable / callbacks you can pass function as an argument