{"action":"created","comment":{"url":"https://api.github.com/repos/cakephp/cakephp/pulls/comments/33424771","id":33424771,"diff_hunk":"@@ -0,0 +1,163 @@\n+<?php\n+/**\n+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)\n+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)\n+ *\n+ * Licensed under The MIT License\n+ * For full copyright and license information, please see the LICENSE.txt\n+ * Redistributions of files must retain the above copyright notice.\n+ *\n+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)\n+ * @link          http://cakephp.org CakePHP(tm) Project\n+ * @since         3.1.0\n+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License\n+ */\n+namespace Cake\\ORM;\n+\n+use Cake\\Collection\\Collection;\n+use Cake\\Database\\Expression\\TupleComparison;\n+use Cake\\Datasource\\EntityInterface;\n+use Cake\\ORM\\Table;\n+\n+/**\n+ * Contains methods that are capable of injecting eagerly loaded associations into\n+ * entities or lists of entities by using the same syntax as the EagerLoader.\n+ *\n+ * @internal\n+ */\n+class LazyEagerLoader\n+{\n+\n+    /**\n+     * Loads the specified associations in the passed entity or list of entities\n+     * by executing extra queries in the database and merging the results in the\n+     * appropriate properties.\n+     *\n+     * The properties for the associations to be loaded will be overwritten on each entity.\n+     *\n+     * @param Cake\\Datasource\\EntityInterface|array $entities a single entity or list of entities\n+     * @param array $contain A `contain()` compatible array.\n+     * @see Cake\\ORM\\Query\\contain()\n+     * @param Cake\\ORM\\Table $source The table to use for fetching the top level entities\n+     * @return Cake\\Datasource\\EntityInterface|array\n+     */\n+    public function loadInto($entities, array $contain, Table $source)\n+    {\n+        $returnSingle = false;\n+\n+        if ($entities instanceof EntityInterface) {\n+            $entities = [$entities];\n+            $returnSingle = true;\n+        }\n+\n+        $entities = new Collection($entities);\n+        $query = $this->_getQuery($entities, $contain, $source);\n+        $associations = array_keys($query->contain());\n+\n+        $entities = $this->_injectResults($entities, $query, $associations, $source);\n+        return $returnSingle ? array_shift($entities) : $entities;\n+    }\n+\n+    /**\n+     * Builds a query for loading the passed list of entity objects along with the\n+     * associations specified in $contain.\n+     *\n+     * @param Cake\\Collection\\CollectionInterface $objects\n+     * @param array $contain The associations to be loaded\n+     * @param Cake\\ORM\\Table $source The table to use for fetching the top level entities\n+     * @return Cake\\ORM\\Query\n+     */\n+    protected function _getQuery($objects, $contain, $source)\n+    {\n+        $primaryKey = $source->primaryKey();\n+        $method = is_string($primaryKey) ? 'get' : 'extract';\n+\n+        $keys = $objects->map(function ($entity) use ($primaryKey, $method) {\n+            return $entity->{$method}($primaryKey);\n+        });\n+\n+        $query = $source\n+            ->find()\n+            ->select((array)$primaryKey)\n+            ->where(function ($exp, $q) use ($primaryKey, $keys, $source) {\n+                if (is_array($primaryKey) && count($primaryKey) === 1) {\n+                    $primaryKey = current($primaryKey);\n+                }\n+\n+                if (is_string($primaryKey)) {\n+                    return $exp->in($source->aliasField($primaryKey), $keys->toList());\n+                }\n+\n+                $types = array_intersect_key($q->defaultTypes(), array_flip($primaryKey));\n+                $primaryKey = array_map([$source, 'aliasField'], $primaryKey);\n+                return new TupleComparison($primaryKey, $keys->toList(), $types, 'IN');\n+            })\n+            ->contain($contain);","path":"src/ORM/LazyEagerLoader.php","position":95,"original_position":95,"commit_id":"dce367df91183352ea3ae0af4d9bb6db1389541b","original_commit_id":"88063bf7076a06a7ed73d4a814a01f2fb8d428e0","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":"Yeah, that is one of the concerns. Mostly that some associations (especially `BelongsTo`) can be defined with conditions dependent on the calling table. I wanted to avoid any invalid sql problems at the cost of an extra query","created_at":"2015-06-28T17:20:16Z","updated_at":"2015-06-28T17:20:16Z","html_url":"https://github.com/cakephp/cakephp/pull/6902#discussion_r33424771","pull_request_url":"https://api.github.com/repos/cakephp/cakephp/pulls/6902","_links":{"self":{"href":"https://api.github.com/repos/cakephp/cakephp/pulls/comments/33424771"},"html":{"href":"https://github.com/cakephp/cakephp/pull/6902#discussion_r33424771"},"pull_request":{"href":"https://api.github.com/repos/cakephp/cakephp/pulls/6902"}}},"pull_request":{"url":"https://api.github.com/repos/cakephp/cakephp/pulls/6902","id":38726474,"html_url":"https://github.com/cakephp/cakephp/pull/6902","diff_url":"https://github.com/cakephp/cakephp/pull/6902.diff","patch_url":"https://github.com/cakephp/cakephp/pull/6902.patch","issue_url":"https://api.github.com/repos/cakephp/cakephp/issues/6902","number":6902,"state":"open","locked":false,"title":"3.1 - Implemented late eager loader of associations","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":"Closes https://github.com/cakephp/cakephp/issues/6104","created_at":"2015-06-28T14:38:53Z","updated_at":"2015-06-28T17:20:16Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5852a5400d5b3eb79dbe42be5e71bdafad4b0ce1","assignee":null,"milestone":{"url":"https://api.github.com/repos/cakephp/cakephp/milestones/31","html_url":"https://github.com/cakephp/cakephp/milestones/3.1.0","labels_url":"https://api.github.com/repos/cakephp/cakephp/milestones/31/labels","id":919718,"number":31,"title":"3.1.0","description":"Backwards compatible with 3.0.x.\r\n\r\nSee the [Roadmap](https://github.com/cakephp/cakephp/wiki/3.1-Roadmap) for more information.","creator":{"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},"open_issues":53,"closed_issues":86,"state":"open","created_at":"2015-01-02T15:38:07Z","updated_at":"2015-06-28T17:04:47Z","due_on":"2015-07-26T04:00:00Z","closed_at":null},"commits_url":"https://api.github.com/repos/cakephp/cakephp/pulls/6902/commits","review_comments_url":"https://api.github.com/repos/cakephp/cakephp/pulls/6902/comments","review_comment_url":"https://api.github.com/repos/cakephp/cakephp/pulls/comments{/number}","comments_url":"https://api.github.com/repos/cakephp/cakephp/issues/6902/comments","statuses_url":"https://api.github.com/repos/cakephp/cakephp/statuses/dce367df91183352ea3ae0af4d9bb6db1389541b","head":{"label":"cakephp:3.1-eager-loader","ref":"3.1-eager-loader","sha":"dce367df91183352ea3ae0af4d9bb6db1389541b","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}","created_at":"2010-05-08T14:38:22Z","updated_at":"2015-06-28T16:01:24Z","pushed_at":"2015-06-28T17:16:50Z","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":239230,"stargazers_count":5889,"watchers_count":5889,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":2654,"mirror_url":null,"open_issues_count":124,"forks":2654,"open_issues":124,"watchers":5889,"default_branch":"master"}},"base":{"label":"cakephp:3.1","ref":"3.1","sha":"17902dbe630977f0062a8d9c3daa0e3bc6a54e26","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}","created_at":"2010-05-08T14:38:22Z","updated_at":"2015-06-28T16:01:24Z","pushed_at":"2015-06-28T17:16:50Z","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":239230,"stargazers_count":5889,"watchers_count":5889,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":2654,"mirror_url":null,"open_issues_count":124,"forks":2654,"open_issues":124,"watchers":5889,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/cakephp/cakephp/pulls/6902"},"html":{"href":"https://github.com/cakephp/cakephp/pull/6902"},"issue":{"href":"https://api.github.com/repos/cakephp/cakephp/issues/6902"},"comments":{"href":"https://api.github.com/repos/cakephp/cakephp/issues/6902/comments"},"review_comments":{"href":"https://api.github.com/repos/cakephp/cakephp/pulls/6902/comments"},"review_comment":{"href":"https://api.github.com/repos/cakephp/cakephp/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/cakephp/cakephp/pulls/6902/commits"},"statuses":{"href":"https://api.github.com/repos/cakephp/cakephp/statuses/dce367df91183352ea3ae0af4d9bb6db1389541b"}}}}