{"action":"created","comment":{"url":"https://api.github.com/repos/cakephp/cakephp/pulls/comments/68491526","id":68491526,"diff_hunk":"@@ -343,30 +347,61 @@ public function attachTo(Query $query, array $options = [])\n \n         // Attach the junction table as well we need it to populate _joinData.\n         $assoc = $this->_targetTable->association($junction->alias());\n-        $query->removeJoin($assoc->name());\n-        $options = array_intersect_key($options, ['joinType' => 1, 'fields' => 1]);\n-        $options += [\n+        $newOptions = array_intersect_key($options, ['joinType' => 1, 'fields' => 1]);\n+        $newOptions += [\n             'conditions' => $cond,\n             'includeFields' => $includeFields,\n-            'foreignKey' => $this->targetForeignKey(),\n+            'foreignKey' => false,\n         ];\n-        $assoc->attachTo($query, $options);\n+        $assoc->attachTo($query, $newOptions);\n         $query->eagerLoader()->addToJoinsMap($junction->alias(), $assoc, true);\n+\n+        parent::attachTo($query, $options);\n+\n+        $foreignKey = $this->targetForeignKey();\n+        $thisJoin = $query->clause('join')[$this->name()];\n+        $thisJoin['conditions']->add($assoc->_joinCondition(['foreignKey' => $foreignKey]));\n     }\n \n     /**\n      * {@inheritDoc}\n      */\n     protected function _appendNotMatching($query, $options)\n     {\n-        $target = $this->junction();\n-        if (!empty($options['negateMatch'])) {\n-            $primaryKey = $query->aliasFields((array)$target->primaryKey(), $target->alias());\n-            $query->andWhere(function ($exp) use ($primaryKey) {\n-                array_map([$exp, 'isNull'], $primaryKey);\n-                return $exp;\n-            });\n+        if (empty($options['negateMatch'])) {\n+            return;\n         }\n+        $options += ['conditions' => []];\n+        $junction = $this->junction();\n+        $belongsTo = $junction->association($this->source()->alias());\n+        $conds = $belongsTo->_joinCondition(['foreignKey' => $belongsTo->foreignKey()]);\n+\n+        $subquery = $this->find()\n+            ->select(array_values($conds))\n+            ->where($options['conditions']);\n+\n+        $subquery = $options['queryBuilder']($subquery);\n+\n+        $assoc = $this->junction()->association($this->target()->alias());\n+        $conditions = $assoc->_joinCondition([\n+            'foreignKey' => $this->targetForeignKey()\n+        ]);\n+        $subquery = $this->_appendJunctionJoin($subquery, $conditions);\n+\n+        $query\n+            ->andWhere(function ($exp) use ($subquery, $conds) {\n+                $identifiers = [];\n+                foreach (array_keys($conds) as $field) {\n+                    $identifiers = new IdentifierExpression($field);","path":"src/ORM/Association/BelongsToMany.php","position":81,"original_position":81,"commit_id":"18ada258472441fa6fc226a6010fbf7607ab5c82","original_commit_id":"18ada258472441fa6fc226a6010fbf7607ab5c82","user":{"login":"lorenzo","id":37621,"avatar_url":"https://avatars.githubusercontent.com/u/37621?v=3","gravatar_id":"","url":"https://api.github.com/users/lorenzo","html_url":"https://github.com/lorenzo","followers_url":"https://api.github.com/users/lorenzo/followers","following_url":"https://api.github.com/users/lorenzo/following{/other_user}","gists_url":"https://api.github.com/users/lorenzo/gists{/gist_id}","starred_url":"https://api.github.com/users/lorenzo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lorenzo/subscriptions","organizations_url":"https://api.github.com/users/lorenzo/orgs","repos_url":"https://api.github.com/users/lorenzo/repos","events_url":"https://api.github.com/users/lorenzo/events{/privacy}","received_events_url":"https://api.github.com/users/lorenzo/received_events","type":"User","site_admin":false},"body":"It does not, but the fields here are always strings. The reason is that each `$field` was a key in the `$conds` array.","created_at":"2016-06-25T17:51:58Z","updated_at":"2016-06-25T17:51:58Z","html_url":"https://github.com/cakephp/cakephp/pull/9036#discussion_r68491526","pull_request_url":"https://api.github.com/repos/cakephp/cakephp/pulls/9036","_links":{"self":{"href":"https://api.github.com/repos/cakephp/cakephp/pulls/comments/68491526"},"html":{"href":"https://github.com/cakephp/cakephp/pull/9036#discussion_r68491526"},"pull_request":{"href":"https://api.github.com/repos/cakephp/cakephp/pulls/9036"}}},"pull_request":{"url":"https://api.github.com/repos/cakephp/cakephp/pulls/9036","id":75210136,"html_url":"https://github.com/cakephp/cakephp/pull/9036","diff_url":"https://github.com/cakephp/cakephp/pull/9036.diff","patch_url":"https://github.com/cakephp/cakephp/pull/9036.patch","issue_url":"https://api.github.com/repos/cakephp/cakephp/issues/9036","number":9036,"state":"open","locked":false,"title":"Invert the order in which the belongsToMany joins are created`","user":{"login":"lorenzo","id":37621,"avatar_url":"https://avatars.githubusercontent.com/u/37621?v=3","gravatar_id":"","url":"https://api.github.com/users/lorenzo","html_url":"https://github.com/lorenzo","followers_url":"https://api.github.com/users/lorenzo/followers","following_url":"https://api.github.com/users/lorenzo/following{/other_user}","gists_url":"https://api.github.com/users/lorenzo/gists{/gist_id}","starred_url":"https://api.github.com/users/lorenzo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lorenzo/subscriptions","organizations_url":"https://api.github.com/users/lorenzo/orgs","repos_url":"https://api.github.com/users/lorenzo/repos","events_url":"https://api.github.com/users/lorenzo/events{/privacy}","received_events_url":"https://api.github.com/users/lorenzo/received_events","type":"User","site_admin":false},"body":"This took me a couple months to get right... but finally here it is.\r\n\r\nIt has been a frequently reported problem the order in which the belongsToMany `matching` joins where created up until this point. For example:\r\n\r\n```sql\r\nselect * from articles inner join tags inner join articles_tags on (...) ...\r\n```\r\n\r\nThe main problems have been:\r\n\r\n1. It was not possible to reference the joint table from the query builder closure:\r\n\r\n```php\r\n$query->matching('tags', function ($q) { $q->where('ArticlesTags.foo is null'); }) // SQL ERROR\r\n```\r\n\r\n2. `leftJoinWith` produces surprisingly odd results, although they are semantically correct.\r\n\r\n```sql\r\n-- $query->leftJoinWith('tags');\r\n\r\nLEFT JOIN tags on 1 = 1 -- This is surprising to some and don't know how to deal with it\r\nLEFT JOIN articles_tags on (...)\r\nWHERE articles_tags.id is null\r\n```\r\n\r\nThe changes in this branch solve both problems by inverting the order of the joins:\r\n\r\n```sql\r\nselect * from articles inner join articles_tags on (...) inner join tags on (...) ...\r\n```\r\n\r\nThese changes are potentially problematic: I'm not really sure what all the consequences for people's code might be. One example of the consequences is that I had to re-implement `notMatching`  for belongs to many associations, given that the new joins order made it impossible to keep the current logic.\r\n\r\nIn the great majority of applications, I would say that the changes are beneficial and would very rarely produce any noticeable change for anyone, but I'm targeting 3.next so that people have the chance to test before we mark it as stable.","created_at":"2016-06-25T17:37:57Z","updated_at":"2016-06-25T17:51:58Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a92c6d599e5135cf36ad39bc37116faa50329fb7","assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/cakephp/cakephp/milestones/69","html_url":"https://github.com/cakephp/cakephp/milestones/3.3.0","labels_url":"https://api.github.com/repos/cakephp/cakephp/milestones/69/labels","id":1443324,"number":69,"title":"3.3.0","description":"A backwards compatible feature release for 3.x. See the [3.3 Roadmap](https://github.com/cakephp/cakephp/wiki/3.3-Roadmap)","creator":{"login":"markstory","id":24086,"avatar_url":"https://avatars.githubusercontent.com/u/24086?v=3","gravatar_id":"","url":"https://api.github.com/users/markstory","html_url":"https://github.com/markstory","followers_url":"https://api.github.com/users/markstory/followers","following_url":"https://api.github.com/users/markstory/following{/other_user}","gists_url":"https://api.github.com/users/markstory/gists{/gist_id}","starred_url":"https://api.github.com/users/markstory/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/markstory/subscriptions","organizations_url":"https://api.github.com/users/markstory/orgs","repos_url":"https://api.github.com/users/markstory/repos","events_url":"https://api.github.com/users/markstory/events{/privacy}","received_events_url":"https://api.github.com/users/markstory/received_events","type":"User","site_admin":false},"open_issues":80,"closed_issues":108,"state":"open","created_at":"2015-12-04T02:24:56Z","updated_at":"2016-06-25T17:38:12Z","due_on":"2016-07-24T04:00:00Z","closed_at":null},"commits_url":"https://api.github.com/repos/cakephp/cakephp/pulls/9036/commits","review_comments_url":"https://api.github.com/repos/cakephp/cakephp/pulls/9036/comments","review_comment_url":"https://api.github.com/repos/cakephp/cakephp/pulls/comments{/number}","comments_url":"https://api.github.com/repos/cakephp/cakephp/issues/9036/comments","statuses_url":"https://api.github.com/repos/cakephp/cakephp/statuses/18ada258472441fa6fc226a6010fbf7607ab5c82","head":{"label":"cakephp:invert-btm-joins","ref":"invert-btm-joins","sha":"18ada258472441fa6fc226a6010fbf7607ab5c82","user":{"login":"cakephp","id":23666,"avatar_url":"https://avatars.githubusercontent.com/u/23666?v=3","gravatar_id":"","url":"https://api.github.com/users/cakephp","html_url":"https://github.com/cakephp","followers_url":"https://api.github.com/users/cakephp/followers","following_url":"https://api.github.com/users/cakephp/following{/other_user}","gists_url":"https://api.github.com/users/cakephp/gists{/gist_id}","starred_url":"https://api.github.com/users/cakephp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cakephp/subscriptions","organizations_url":"https://api.github.com/users/cakephp/orgs","repos_url":"https://api.github.com/users/cakephp/repos","events_url":"https://api.github.com/users/cakephp/events{/privacy}","received_events_url":"https://api.github.com/users/cakephp/received_events","type":"Organization","site_admin":false},"repo":{"id":656494,"name":"cakephp","full_name":"cakephp/cakephp","owner":{"login":"cakephp","id":23666,"avatar_url":"https://avatars.githubusercontent.com/u/23666?v=3","gravatar_id":"","url":"https://api.github.com/users/cakephp","html_url":"https://github.com/cakephp","followers_url":"https://api.github.com/users/cakephp/followers","following_url":"https://api.github.com/users/cakephp/following{/other_user}","gists_url":"https://api.github.com/users/cakephp/gists{/gist_id}","starred_url":"https://api.github.com/users/cakephp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cakephp/subscriptions","organizations_url":"https://api.github.com/users/cakephp/orgs","repos_url":"https://api.github.com/users/cakephp/repos","events_url":"https://api.github.com/users/cakephp/events{/privacy}","received_events_url":"https://api.github.com/users/cakephp/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cakephp/cakephp","description":"CakePHP: The Rapid Development Framework for PHP - Official Repository","fork":false,"url":"https://api.github.com/repos/cakephp/cakephp","forks_url":"https://api.github.com/repos/cakephp/cakephp/forks","keys_url":"https://api.github.com/repos/cakephp/cakephp/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cakephp/cakephp/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cakephp/cakephp/teams","hooks_url":"https://api.github.com/repos/cakephp/cakephp/hooks","issue_events_url":"https://api.github.com/repos/cakephp/cakephp/issues/events{/number}","events_url":"https://api.github.com/repos/cakephp/cakephp/events","assignees_url":"https://api.github.com/repos/cakephp/cakephp/assignees{/user}","branches_url":"https://api.github.com/repos/cakephp/cakephp/branches{/branch}","tags_url":"https://api.github.com/repos/cakephp/cakephp/tags","blobs_url":"https://api.github.com/repos/cakephp/cakephp/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cakephp/cakephp/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cakephp/cakephp/git/refs{/sha}","trees_url":"https://api.github.com/repos/cakephp/cakephp/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cakephp/cakephp/statuses/{sha}","languages_url":"https://api.github.com/repos/cakephp/cakephp/languages","stargazers_url":"https://api.github.com/repos/cakephp/cakephp/stargazers","contributors_url":"https://api.github.com/repos/cakephp/cakephp/contributors","subscribers_url":"https://api.github.com/repos/cakephp/cakephp/subscribers","subscription_url":"https://api.github.com/repos/cakephp/cakephp/subscription","commits_url":"https://api.github.com/repos/cakephp/cakephp/commits{/sha}","git_commits_url":"https://api.github.com/repos/cakephp/cakephp/git/commits{/sha}","comments_url":"https://api.github.com/repos/cakephp/cakephp/comments{/number}","issue_comment_url":"https://api.github.com/repos/cakephp/cakephp/issues/comments{/number}","contents_url":"https://api.github.com/repos/cakephp/cakephp/contents/{+path}","compare_url":"https://api.github.com/repos/cakephp/cakephp/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cakephp/cakephp/merges","archive_url":"https://api.github.com/repos/cakephp/cakephp/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cakephp/cakephp/downloads","issues_url":"https://api.github.com/repos/cakephp/cakephp/issues{/number}","pulls_url":"https://api.github.com/repos/cakephp/cakephp/pulls{/number}","milestones_url":"https://api.github.com/repos/cakephp/cakephp/milestones{/number}","notifications_url":"https://api.github.com/repos/cakephp/cakephp/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cakephp/cakephp/labels{/name}","releases_url":"https://api.github.com/repos/cakephp/cakephp/releases{/id}","deployments_url":"https://api.github.com/repos/cakephp/cakephp/deployments","created_at":"2010-05-08T14:38:22Z","updated_at":"2016-06-25T11:48:35Z","pushed_at":"2016-06-25T17:37:57Z","git_url":"git://github.com/cakephp/cakephp.git","ssh_url":"git@github.com:cakephp/cakephp.git","clone_url":"https://github.com/cakephp/cakephp.git","svn_url":"https://github.com/cakephp/cakephp","homepage":"http://cakephp.org","size":129316,"stargazers_count":6564,"watchers_count":6564,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":2981,"mirror_url":null,"open_issues_count":204,"forks":2981,"open_issues":204,"watchers":6564,"default_branch":"master"}},"base":{"label":"cakephp:3.next","ref":"3.next","sha":"4aab2bf0cd814a202350f6ee93753838ea6b1274","user":{"login":"cakephp","id":23666,"avatar_url":"https://avatars.githubusercontent.com/u/23666?v=3","gravatar_id":"","url":"https://api.github.com/users/cakephp","html_url":"https://github.com/cakephp","followers_url":"https://api.github.com/users/cakephp/followers","following_url":"https://api.github.com/users/cakephp/following{/other_user}","gists_url":"https://api.github.com/users/cakephp/gists{/gist_id}","starred_url":"https://api.github.com/users/cakephp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cakephp/subscriptions","organizations_url":"https://api.github.com/users/cakephp/orgs","repos_url":"https://api.github.com/users/cakephp/repos","events_url":"https://api.github.com/users/cakephp/events{/privacy}","received_events_url":"https://api.github.com/users/cakephp/received_events","type":"Organization","site_admin":false},"repo":{"id":656494,"name":"cakephp","full_name":"cakephp/cakephp","owner":{"login":"cakephp","id":23666,"avatar_url":"https://avatars.githubusercontent.com/u/23666?v=3","gravatar_id":"","url":"https://api.github.com/users/cakephp","html_url":"https://github.com/cakephp","followers_url":"https://api.github.com/users/cakephp/followers","following_url":"https://api.github.com/users/cakephp/following{/other_user}","gists_url":"https://api.github.com/users/cakephp/gists{/gist_id}","starred_url":"https://api.github.com/users/cakephp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cakephp/subscriptions","organizations_url":"https://api.github.com/users/cakephp/orgs","repos_url":"https://api.github.com/users/cakephp/repos","events_url":"https://api.github.com/users/cakephp/events{/privacy}","received_events_url":"https://api.github.com/users/cakephp/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cakephp/cakephp","description":"CakePHP: The Rapid Development Framework for PHP - Official Repository","fork":false,"url":"https://api.github.com/repos/cakephp/cakephp","forks_url":"https://api.github.com/repos/cakephp/cakephp/forks","keys_url":"https://api.github.com/repos/cakephp/cakephp/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cakephp/cakephp/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cakephp/cakephp/teams","hooks_url":"https://api.github.com/repos/cakephp/cakephp/hooks","issue_events_url":"https://api.github.com/repos/cakephp/cakephp/issues/events{/number}","events_url":"https://api.github.com/repos/cakephp/cakephp/events","assignees_url":"https://api.github.com/repos/cakephp/cakephp/assignees{/user}","branches_url":"https://api.github.com/repos/cakephp/cakephp/branches{/branch}","tags_url":"https://api.github.com/repos/cakephp/cakephp/tags","blobs_url":"https://api.github.com/repos/cakephp/cakephp/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cakephp/cakephp/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cakephp/cakephp/git/refs{/sha}","trees_url":"https://api.github.com/repos/cakephp/cakephp/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cakephp/cakephp/statuses/{sha}","languages_url":"https://api.github.com/repos/cakephp/cakephp/languages","stargazers_url":"https://api.github.com/repos/cakephp/cakephp/stargazers","contributors_url":"https://api.github.com/repos/cakephp/cakephp/contributors","subscribers_url":"https://api.github.com/repos/cakephp/cakephp/subscribers","subscription_url":"https://api.github.com/repos/cakephp/cakephp/subscription","commits_url":"https://api.github.com/repos/cakephp/cakephp/commits{/sha}","git_commits_url":"https://api.github.com/repos/cakephp/cakephp/git/commits{/sha}","comments_url":"https://api.github.com/repos/cakephp/cakephp/comments{/number}","issue_comment_url":"https://api.github.com/repos/cakephp/cakephp/issues/comments{/number}","contents_url":"https://api.github.com/repos/cakephp/cakephp/contents/{+path}","compare_url":"https://api.github.com/repos/cakephp/cakephp/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cakephp/cakephp/merges","archive_url":"https://api.github.com/repos/cakephp/cakephp/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cakephp/cakephp/downloads","issues_url":"https://api.github.com/repos/cakephp/cakephp/issues{/number}","pulls_url":"https://api.github.com/repos/cakephp/cakephp/pulls{/number}","milestones_url":"https://api.github.com/repos/cakephp/cakephp/milestones{/number}","notifications_url":"https://api.github.com/repos/cakephp/cakephp/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cakephp/cakephp/labels{/name}","releases_url":"https://api.github.com/repos/cakephp/cakephp/releases{/id}","deployments_url":"https://api.github.com/repos/cakephp/cakephp/deployments","created_at":"2010-05-08T14:38:22Z","updated_at":"2016-06-25T11:48:35Z","pushed_at":"2016-06-25T17:37:57Z","git_url":"git://github.com/cakephp/cakephp.git","ssh_url":"git@github.com:cakephp/cakephp.git","clone_url":"https://github.com/cakephp/cakephp.git","svn_url":"https://github.com/cakephp/cakephp","homepage":"http://cakephp.org","size":129316,"stargazers_count":6564,"watchers_count":6564,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":2981,"mirror_url":null,"open_issues_count":204,"forks":2981,"open_issues":204,"watchers":6564,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/cakephp/cakephp/pulls/9036"},"html":{"href":"https://github.com/cakephp/cakephp/pull/9036"},"issue":{"href":"https://api.github.com/repos/cakephp/cakephp/issues/9036"},"comments":{"href":"https://api.github.com/repos/cakephp/cakephp/issues/9036/comments"},"review_comments":{"href":"https://api.github.com/repos/cakephp/cakephp/pulls/9036/comments"},"review_comment":{"href":"https://api.github.com/repos/cakephp/cakephp/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/cakephp/cakephp/pulls/9036/commits"},"statuses":{"href":"https://api.github.com/repos/cakephp/cakephp/statuses/18ada258472441fa6fc226a6010fbf7607ab5c82"}}}}
{"actor":{"display_login":"lorenzo"}}