{"action":"created","comment":{"url":"https://api.github.com/repos/cakephp/docs/pulls/comments/29993840","id":29993840,"diff_hunk":"@@ -589,6 +589,72 @@ association properties in your results.\n \n .. end-contain\n \n+Changing Fetching Strategies\n+----------------------------\n+\n+As you may know already, ``belongsTo`` and ``hasOne`` associations are loaded\n+using a ``JOIN`` in the main finder query. While this improves query and\n+fetching speed and allows for creating more expressive conditions when\n+retrieving data, this may be a problem when you want to apply certain clauses to\n+the finder query for the association, such as ``order()`` or ``limit()``.\n+\n+Fore example, imagine you wanted to have the first comment of an article as an\n+association::\n+\n+   $articles->hasOne('FirstComment', [\n+        'className' => 'Comments',\n+        'foreignKey' => 'article_id'\n+   ]);\n+\n+In order to correctly fetch the data from this association, we will need to tell\n+the query to use the ``select`` strategy, since we want order by a particular\n+column::\n+\n+    $query = $articles->find()->contain([\n+        'FirstComment' => [\n+                'strategy' => 'select',\n+                'queryBuilder' => function ($q) {\n+                    return $q->order(['FirstComment.created' =>'ASC'])->limit(1);\n+                }\n+        ]\n+    ]);\n+\n+Dynamically changing the strategy in this way will only apply to a specific\n+query. If you want to make the strategy change permanent you can do::\n+\n+    $articles->FirstComment->strategy('select');\n+\n+Using the ``select`` strategy is also a great way of making associations with\n+tables in another database, since it would not be possible to fetch records\n+using ``joins``.\n+\n+Fetching With The Subquery Strategy\n+-----------------------------------\n+\n+As your tables grow in since, fetching associations from them can become\n+slower, especially if you are querying big batches at once. A good way of\n+optimizing association loading for ``hasMany`` and ``belongsToMany``\n+associations is using the ``subquery`` strategy::\n+\n+    $query = $articles->find()->contain([\n+        'Comments' => [\n+                'strategy' => 'subquery',\n+                'queryBuilder' => function ($q) {\n+                    return $q->where(['Comments.approved' => true]);\n+                }\n+        ]\n+    ]);\n+\n+The result will remain the same as with using the default strategy, but this\n+can greatly improve the query and fetching time in some databases, in\n+particular it will allow to fetch big chunks of data at the same time in\n+databases that limit the amount of bound parameters per query, such as\n+**Microfost SQL Server**.","path":"en/orm/retrieving-data-and-resultsets.rst","position":64,"original_position":64,"commit_id":"7f64ac7b15d18dc3078dbed4fae034f023c1577d","original_commit_id":"7f64ac7b15d18dc3078dbed4fae034f023c1577d","user":{"login":"dereuromark","id":39854,"avatar_url":"https://avatars.githubusercontent.com/u/39854?v=3","gravatar_id":"","url":"https://api.github.com/users/dereuromark","html_url":"https://github.com/dereuromark","followers_url":"https://api.github.com/users/dereuromark/followers","following_url":"https://api.github.com/users/dereuromark/following{/other_user}","gists_url":"https://api.github.com/users/dereuromark/gists{/gist_id}","starred_url":"https://api.github.com/users/dereuromark/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dereuromark/subscriptions","organizations_url":"https://api.github.com/users/dereuromark/orgs","repos_url":"https://api.github.com/users/dereuromark/repos","events_url":"https://api.github.com/users/dereuromark/events{/privacy}","received_events_url":"https://api.github.com/users/dereuromark/received_events","type":"User","site_admin":false},"body":"fost vs soft :)","created_at":"2015-05-09T16:12:43Z","updated_at":"2015-05-09T16:12:43Z","html_url":"https://github.com/cakephp/docs/pull/2716#discussion_r29993840","pull_request_url":"https://api.github.com/repos/cakephp/docs/pulls/2716","_links":{"self":{"href":"https://api.github.com/repos/cakephp/docs/pulls/comments/29993840"},"html":{"href":"https://github.com/cakephp/docs/pull/2716#discussion_r29993840"},"pull_request":{"href":"https://api.github.com/repos/cakephp/docs/pulls/2716"}}},"pull_request":{"url":"https://api.github.com/repos/cakephp/docs/pulls/2716","id":35068735,"html_url":"https://github.com/cakephp/docs/pull/2716","diff_url":"https://github.com/cakephp/docs/pull/2716.diff","patch_url":"https://github.com/cakephp/docs/pull/2716.patch","issue_url":"https://api.github.com/repos/cakephp/docs/issues/2716","number":2716,"state":"open","locked":false,"title":"Documenting the select and subquery strategy","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":"improvement over https://github.com/cakephp/docs/pull/2711 and https://github.com/cakephp/docs/pull/2710","created_at":"2015-05-09T16:04:05Z","updated_at":"2015-05-09T16:12:43Z","closed_at":null,"merged_at":null,"merge_commit_sha":"dd6c006ce2bfd050d4cc0e21e2d2b7cd4005392a","assignee":null,"milestone":{"url":"https://api.github.com/repos/cakephp/docs/milestones/1","html_url":"https://github.com/cakephp/docs/milestones/3.0","labels_url":"https://api.github.com/repos/cakephp/docs/milestones/1/labels","id":629438,"number":1,"title":"3.0","description":"","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":10,"closed_issues":880,"state":"open","created_at":"2014-04-14T15:44:13Z","updated_at":"2015-05-09T16:04:16Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/cakephp/docs/pulls/2716/commits","review_comments_url":"https://api.github.com/repos/cakephp/docs/pulls/2716/comments","review_comment_url":"https://api.github.com/repos/cakephp/docs/pulls/comments{/number}","comments_url":"https://api.github.com/repos/cakephp/docs/issues/2716/comments","statuses_url":"https://api.github.com/repos/cakephp/docs/statuses/7f64ac7b15d18dc3078dbed4fae034f023c1577d","head":{"label":"cakephp:caBBAlainB-patch-42","ref":"caBBAlainB-patch-42","sha":"7f64ac7b15d18dc3078dbed4fae034f023c1577d","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":1514950,"name":"docs","full_name":"cakephp/docs","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/docs","description":"CakePHP CookBook","fork":false,"url":"https://api.github.com/repos/cakephp/docs","forks_url":"https://api.github.com/repos/cakephp/docs/forks","keys_url":"https://api.github.com/repos/cakephp/docs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cakephp/docs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cakephp/docs/teams","hooks_url":"https://api.github.com/repos/cakephp/docs/hooks","issue_events_url":"https://api.github.com/repos/cakephp/docs/issues/events{/number}","events_url":"https://api.github.com/repos/cakephp/docs/events","assignees_url":"https://api.github.com/repos/cakephp/docs/assignees{/user}","branches_url":"https://api.github.com/repos/cakephp/docs/branches{/branch}","tags_url":"https://api.github.com/repos/cakephp/docs/tags","blobs_url":"https://api.github.com/repos/cakephp/docs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cakephp/docs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cakephp/docs/git/refs{/sha}","trees_url":"https://api.github.com/repos/cakephp/docs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cakephp/docs/statuses/{sha}","languages_url":"https://api.github.com/repos/cakephp/docs/languages","stargazers_url":"https://api.github.com/repos/cakephp/docs/stargazers","contributors_url":"https://api.github.com/repos/cakephp/docs/contributors","subscribers_url":"https://api.github.com/repos/cakephp/docs/subscribers","subscription_url":"https://api.github.com/repos/cakephp/docs/subscription","commits_url":"https://api.github.com/repos/cakephp/docs/commits{/sha}","git_commits_url":"https://api.github.com/repos/cakephp/docs/git/commits{/sha}","comments_url":"https://api.github.com/repos/cakephp/docs/comments{/number}","issue_comment_url":"https://api.github.com/repos/cakephp/docs/issues/comments{/number}","contents_url":"https://api.github.com/repos/cakephp/docs/contents/{+path}","compare_url":"https://api.github.com/repos/cakephp/docs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cakephp/docs/merges","archive_url":"https://api.github.com/repos/cakephp/docs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cakephp/docs/downloads","issues_url":"https://api.github.com/repos/cakephp/docs/issues{/number}","pulls_url":"https://api.github.com/repos/cakephp/docs/pulls{/number}","milestones_url":"https://api.github.com/repos/cakephp/docs/milestones{/number}","notifications_url":"https://api.github.com/repos/cakephp/docs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cakephp/docs/labels{/name}","releases_url":"https://api.github.com/repos/cakephp/docs/releases{/id}","created_at":"2011-03-23T04:54:13Z","updated_at":"2015-05-08T18:20:32Z","pushed_at":"2015-05-09T16:04:05Z","git_url":"git://github.com/cakephp/docs.git","ssh_url":"git@github.com:cakephp/docs.git","clone_url":"https://github.com/cakephp/docs.git","svn_url":"https://github.com/cakephp/docs","homepage":"http://book.cakephp.org","size":55918,"stargazers_count":462,"watchers_count":462,"language":"Makefile","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1307,"mirror_url":null,"open_issues_count":23,"forks":1307,"open_issues":23,"watchers":462,"default_branch":"master"}},"base":{"label":"cakephp:3.0","ref":"3.0","sha":"2cb9b9b50497981dffc67af2b7c5b3ce9b3fc39a","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":1514950,"name":"docs","full_name":"cakephp/docs","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/docs","description":"CakePHP CookBook","fork":false,"url":"https://api.github.com/repos/cakephp/docs","forks_url":"https://api.github.com/repos/cakephp/docs/forks","keys_url":"https://api.github.com/repos/cakephp/docs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cakephp/docs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cakephp/docs/teams","hooks_url":"https://api.github.com/repos/cakephp/docs/hooks","issue_events_url":"https://api.github.com/repos/cakephp/docs/issues/events{/number}","events_url":"https://api.github.com/repos/cakephp/docs/events","assignees_url":"https://api.github.com/repos/cakephp/docs/assignees{/user}","branches_url":"https://api.github.com/repos/cakephp/docs/branches{/branch}","tags_url":"https://api.github.com/repos/cakephp/docs/tags","blobs_url":"https://api.github.com/repos/cakephp/docs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cakephp/docs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cakephp/docs/git/refs{/sha}","trees_url":"https://api.github.com/repos/cakephp/docs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cakephp/docs/statuses/{sha}","languages_url":"https://api.github.com/repos/cakephp/docs/languages","stargazers_url":"https://api.github.com/repos/cakephp/docs/stargazers","contributors_url":"https://api.github.com/repos/cakephp/docs/contributors","subscribers_url":"https://api.github.com/repos/cakephp/docs/subscribers","subscription_url":"https://api.github.com/repos/cakephp/docs/subscription","commits_url":"https://api.github.com/repos/cakephp/docs/commits{/sha}","git_commits_url":"https://api.github.com/repos/cakephp/docs/git/commits{/sha}","comments_url":"https://api.github.com/repos/cakephp/docs/comments{/number}","issue_comment_url":"https://api.github.com/repos/cakephp/docs/issues/comments{/number}","contents_url":"https://api.github.com/repos/cakephp/docs/contents/{+path}","compare_url":"https://api.github.com/repos/cakephp/docs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cakephp/docs/merges","archive_url":"https://api.github.com/repos/cakephp/docs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cakephp/docs/downloads","issues_url":"https://api.github.com/repos/cakephp/docs/issues{/number}","pulls_url":"https://api.github.com/repos/cakephp/docs/pulls{/number}","milestones_url":"https://api.github.com/repos/cakephp/docs/milestones{/number}","notifications_url":"https://api.github.com/repos/cakephp/docs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cakephp/docs/labels{/name}","releases_url":"https://api.github.com/repos/cakephp/docs/releases{/id}","created_at":"2011-03-23T04:54:13Z","updated_at":"2015-05-08T18:20:32Z","pushed_at":"2015-05-09T16:04:05Z","git_url":"git://github.com/cakephp/docs.git","ssh_url":"git@github.com:cakephp/docs.git","clone_url":"https://github.com/cakephp/docs.git","svn_url":"https://github.com/cakephp/docs","homepage":"http://book.cakephp.org","size":55918,"stargazers_count":462,"watchers_count":462,"language":"Makefile","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1307,"mirror_url":null,"open_issues_count":23,"forks":1307,"open_issues":23,"watchers":462,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/cakephp/docs/pulls/2716"},"html":{"href":"https://github.com/cakephp/docs/pull/2716"},"issue":{"href":"https://api.github.com/repos/cakephp/docs/issues/2716"},"comments":{"href":"https://api.github.com/repos/cakephp/docs/issues/2716/comments"},"review_comments":{"href":"https://api.github.com/repos/cakephp/docs/pulls/2716/comments"},"review_comment":{"href":"https://api.github.com/repos/cakephp/docs/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/cakephp/docs/pulls/2716/commits"},"statuses":{"href":"https://api.github.com/repos/cakephp/docs/statuses/7f64ac7b15d18dc3078dbed4fae034f023c1577d"}}}}