{"action":"created","issue":{"url":"https://api.github.com/repos/cakephp/cakephp/issues/7171","labels_url":"https://api.github.com/repos/cakephp/cakephp/issues/7171/labels{/name}","comments_url":"https://api.github.com/repos/cakephp/cakephp/issues/7171/comments","events_url":"https://api.github.com/repos/cakephp/cakephp/issues/7171/events","html_url":"https://github.com/cakephp/cakephp/issues/7171","id":98978965,"number":7171,"title":"2.x: Containable retrieves too much data","user":{"login":"janriokrause","id":1068037,"avatar_url":"https://avatars.githubusercontent.com/u/1068037?v=3","gravatar_id":"","url":"https://api.github.com/users/janriokrause","html_url":"https://github.com/janriokrause","followers_url":"https://api.github.com/users/janriokrause/followers","following_url":"https://api.github.com/users/janriokrause/following{/other_user}","gists_url":"https://api.github.com/users/janriokrause/gists{/gist_id}","starred_url":"https://api.github.com/users/janriokrause/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/janriokrause/subscriptions","organizations_url":"https://api.github.com/users/janriokrause/orgs","repos_url":"https://api.github.com/users/janriokrause/repos","events_url":"https://api.github.com/users/janriokrause/events{/privacy}","received_events_url":"https://api.github.com/users/janriokrause/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/cakephp/cakephp/labels/On%20hold","name":"On hold","color":"eb6420"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/cakephp/cakephp/milestones/62","html_url":"https://github.com/cakephp/cakephp/milestones/2.7.6","labels_url":"https://api.github.com/repos/cakephp/cakephp/milestones/62/labels","id":1334179,"number":62,"title":"2.7.6","description":"Bugfixes and optimizations for the 2.7 release branch","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":16,"closed_issues":17,"state":"open","created_at":"2015-10-02T00:57:17Z","updated_at":"2015-10-30T18:31:21Z","due_on":"2015-11-01T04:00:00Z","closed_at":null},"comments":13,"created_at":"2015-08-04T14:09:02Z","updated_at":"2015-10-31T16:07:34Z","closed_at":null,"body":"Containable will retrieve a lot of more data than requested under certain circumstances. Take a look at the following example with the [database schema](http://pastebin.com/yQEb9Bje). I have simplified the whole code and associations to a minimum to reproduce the bug.\r\n\r\nAfter some tests, I figured out that the bug first appeared from `v2.1.1` to `v2.1.2`.\r\n\r\n```php\r\n<?php\r\nApp::uses('Model', 'Model');\r\nclass AppModel extends Model {\r\n\r\n\tpublic $actsAs = [\r\n\t\t'Containable',\r\n\t];\r\n\r\n}\r\n```\r\n\r\n```php\r\n<?php\r\nclass AgeGroup extends AppModel {\r\n\r\n\tpublic $hasMany = [\r\n\t\t'Result',\r\n\t];\r\n\r\n}\r\n```\r\n\r\n```php\r\n<?php\r\nclass Competition extends AppModel {\r\n\r\n\tpublic $hasMany = [\r\n\t\t'Result',\r\n\t];\r\n\r\n\tpublic $hasAndBelongsToMany = [\r\n\t\t'AgeGroup',\r\n\t];\r\n\r\n}\r\n```\r\n\r\n```php\r\n<?php\r\nclass Event extends AppModel {\r\n\r\n\tpublic $hasAndBelongsToMany = [\r\n\t\t'Competition',\r\n\t];\r\n\r\n}\r\n```\r\n\r\n```php\r\n<?php\r\nclass Result extends AppModel {\r\n\r\n\tpublic $belongsTo = [\r\n\t\t'AgeGroup',\r\n\t\t'Event',\r\n\t];\r\n\r\n\tpublic function afterFind($results, $primary = false) {\r\n\t\tforeach ($results as $key => $value) {\r\n\t\t\t// sometimes I need to access details of the event. I cannot use $value['Event'] since it is not set, so I query it myself.\r\n\t\t\t$this->Event->id = $value['Result']['event_id'];\r\n\t\t\t$name = $this->Event->field('name'); // try commenting out the entire line and see the changes happen in the ouput\r\n\t\t}\r\n\t\treturn $results;\r\n\t}\r\n\r\n}\r\n```\r\n\r\n```php\r\n<?php\r\nclass CompetitionsController extends AppController {\r\n\r\n\tpublic function index() {\r\n\t\tdebug($this->Competition->find('first', [\r\n\t\t\t'conditions' => ['id' => 1],\r\n\t\t\t'contain' => [\r\n\t\t\t\t'Result' => [\r\n\t\t\t\t\t'AgeGroup',\r\n\t\t\t\t],\r\n\t\t\t],\r\n\t\t]));\r\n\t}\r\n\r\n}\r\n```\r\n\r\nDesired output of controller:\r\n\r\n```php\r\narray(\r\n\t'Competition' => array(\r\n\t\t'id' => '1',\r\n\t\t'name' => 'test'\r\n\t),\r\n\t'Result' => array(\r\n\t\t(int) 0 => array(\r\n\t\t\t'id' => '1',\r\n\t\t\t'age_group_id' => '1',\r\n\t\t\t'competition_id' => '1',\r\n\t\t\t'event_id' => '1',\r\n\t\t\t'AgeGroup' => array(\r\n\t\t\t\t'id' => '1',\r\n\t\t\t\t'name' => 'test'\r\n\t\t\t)\r\n\t\t)\r\n\t)\r\n)\r\n```\r\n\r\nActual output of Controller:\r\n\r\n```php\r\narray(\r\n\t'Competition' => array(\r\n\t\t'id' => '1',\r\n\t\t'name' => 'test'\r\n\t),\r\n\t'Result' => array(\r\n\t\t(int) 0 => array(\r\n\t\t\t'id' => '1',\r\n\t\t\t'age_group_id' => '1',\r\n\t\t\t'competition_id' => '1',\r\n\t\t\t'event_id' => '1',\r\n\t\t\t'AgeGroup' => array(\r\n\t\t\t\t'id' => '1',\r\n\t\t\t\t'name' => 'test'\r\n\t\t\t)\r\n\t\t)\r\n\t),\r\n\t'AgeGroup' => array(\r\n\t\t(int) 0 => array(\r\n\t\t\t'id' => '1',\r\n\t\t\t'name' => 'test',\r\n\t\t\t'AgeGroupsCompetition' => array(\r\n\t\t\t\t'id' => '1',\r\n\t\t\t\t'age_group_id' => '1',\r\n\t\t\t\t'competition_id' => '1'\r\n\t\t\t),\r\n\t\t\t'Result' => array(\r\n\t\t\t\t(int) 0 => array(\r\n\t\t\t\t\t'id' => '1',\r\n\t\t\t\t\t'age_group_id' => '1',\r\n\t\t\t\t\t'competition_id' => '1',\r\n\t\t\t\t\t'event_id' => '1'\r\n\t\t\t\t),\r\n\t\t\t\t(int) 1 => array(\r\n\t\t\t\t\t'id' => '2',\r\n\t\t\t\t\t'age_group_id' => '1',\r\n\t\t\t\t\t'competition_id' => '2',\r\n\t\t\t\t\t'event_id' => '1'\r\n\t\t\t\t),\r\n\t\t\t\t(int) 2 => array(\r\n\t\t\t\t\t'id' => '3',\r\n\t\t\t\t\t'age_group_id' => '1',\r\n\t\t\t\t\t'competition_id' => '2',\r\n\t\t\t\t\t'event_id' => '1'\r\n\t\t\t\t),\r\n\t\t\t\t// ...\r\n\t\t\t\t// ...\r\n\t\t\t\t// ...\r\n\t\t\t\t(int) 99 => array(\r\n\t\t\t\t\t'id' => '100',\r\n\t\t\t\t\t'age_group_id' => '1',\r\n\t\t\t\t\t'competition_id' => '2',\r\n\t\t\t\t\t'event_id' => '1'\r\n\t\t\t\t)\r\n\t\t),\r\n\t\t(int) 1 => array(\r\n\t\t\t// If there are other age groups linked to competition 1 this data is fetched, too\r\n\t\t\t// this also includes all results linked to the age group just fetched\r\n\t\t)\r\n\t)\r\n)\r\n```"},"comment":{"url":"https://api.github.com/repos/cakephp/cakephp/issues/comments/152745671","html_url":"https://github.com/cakephp/cakephp/issues/7171#issuecomment-152745671","issue_url":"https://api.github.com/repos/cakephp/cakephp/issues/7171","id":152745671,"user":{"login":"chinpei215","id":7399393,"avatar_url":"https://avatars.githubusercontent.com/u/7399393?v=3","gravatar_id":"","url":"https://api.github.com/users/chinpei215","html_url":"https://github.com/chinpei215","followers_url":"https://api.github.com/users/chinpei215/followers","following_url":"https://api.github.com/users/chinpei215/following{/other_user}","gists_url":"https://api.github.com/users/chinpei215/gists{/gist_id}","starred_url":"https://api.github.com/users/chinpei215/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chinpei215/subscriptions","organizations_url":"https://api.github.com/users/chinpei215/orgs","repos_url":"https://api.github.com/users/chinpei215/repos","events_url":"https://api.github.com/users/chinpei215/events{/privacy}","received_events_url":"https://api.github.com/users/chinpei215/received_events","type":"User","site_admin":false},"created_at":"2015-10-31T16:07:34Z","updated_at":"2015-10-31T16:07:34Z","body":"@janriokrause I tried to reproduce this issue now, but I couldn't. I've got expected results. So what version of PHP are you using? It seems to me that caused by broken reference."}}