I have a few questions about Join in Cakephp

Question

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


Show source
| cakephp   | cakephp-3.0   2017-01-04 03:01 1 Answers

Answers to I have a few questions about Join in Cakephp ( 1 )

  1. 2017-01-05 07:01

    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 enter code here 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 apply 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

Leave a reply to - I have a few questions about Join in Cakephp

◀ Go back