{"action":"created","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/5341","repository_url":"https://api.github.com/repos/yiisoft/yii2","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/5341/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/5341/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/5341/events","html_url":"https://github.com/yiisoft/yii2/issues/5341","id":44832631,"number":5341,"title":"HasMany via two relations","user":{"login":"angelcoding","id":685911,"avatar_url":"https://avatars.githubusercontent.com/u/685911?v=3","gravatar_id":"","url":"https://api.github.com/users/angelcoding","html_url":"https://github.com/angelcoding","followers_url":"https://api.github.com/users/angelcoding/followers","following_url":"https://api.github.com/users/angelcoding/following{/other_user}","gists_url":"https://api.github.com/users/angelcoding/gists{/gist_id}","starred_url":"https://api.github.com/users/angelcoding/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/angelcoding/subscriptions","organizations_url":"https://api.github.com/users/angelcoding/orgs","repos_url":"https://api.github.com/users/angelcoding/repos","events_url":"https://api.github.com/users/angelcoding/events{/privacy}","received_events_url":"https://api.github.com/users/angelcoding/received_events","type":"User","site_admin":false},"labels":[{"id":80755682,"url":"https://api.github.com/repos/yiisoft/yii2/labels/complexity:hard","name":"complexity:hard","color":"fad8c7","default":false},{"id":33208443,"url":"https://api.github.com/repos/yiisoft/yii2/labels/type:bug","name":"type:bug","color":"e102d8","default":false}],"state":"open","locked":false,"assignee":{"login":"cebe","id":189796,"avatar_url":"https://avatars.githubusercontent.com/u/189796?v=3","gravatar_id":"","url":"https://api.github.com/users/cebe","html_url":"https://github.com/cebe","followers_url":"https://api.github.com/users/cebe/followers","following_url":"https://api.github.com/users/cebe/following{/other_user}","gists_url":"https://api.github.com/users/cebe/gists{/gist_id}","starred_url":"https://api.github.com/users/cebe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cebe/subscriptions","organizations_url":"https://api.github.com/users/cebe/orgs","repos_url":"https://api.github.com/users/cebe/repos","events_url":"https://api.github.com/users/cebe/events{/privacy}","received_events_url":"https://api.github.com/users/cebe/received_events","type":"User","site_admin":false},"assignees":[{"login":"cebe","id":189796,"avatar_url":"https://avatars.githubusercontent.com/u/189796?v=3","gravatar_id":"","url":"https://api.github.com/users/cebe","html_url":"https://github.com/cebe","followers_url":"https://api.github.com/users/cebe/followers","following_url":"https://api.github.com/users/cebe/following{/other_user}","gists_url":"https://api.github.com/users/cebe/gists{/gist_id}","starred_url":"https://api.github.com/users/cebe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cebe/subscriptions","organizations_url":"https://api.github.com/users/cebe/orgs","repos_url":"https://api.github.com/users/cebe/repos","events_url":"https://api.github.com/users/cebe/events{/privacy}","received_events_url":"https://api.github.com/users/cebe/received_events","type":"User","site_admin":false}],"milestone":{"url":"https://api.github.com/repos/yiisoft/yii2/milestones/21","html_url":"https://github.com/yiisoft/yii2/milestone/21","labels_url":"https://api.github.com/repos/yiisoft/yii2/milestones/21/labels","id":1875845,"number":21,"title":"2.0.12","description":"","creator":{"login":"cebe","id":189796,"avatar_url":"https://avatars.githubusercontent.com/u/189796?v=3","gravatar_id":"","url":"https://api.github.com/users/cebe","html_url":"https://github.com/cebe","followers_url":"https://api.github.com/users/cebe/followers","following_url":"https://api.github.com/users/cebe/following{/other_user}","gists_url":"https://api.github.com/users/cebe/gists{/gist_id}","starred_url":"https://api.github.com/users/cebe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cebe/subscriptions","organizations_url":"https://api.github.com/users/cebe/orgs","repos_url":"https://api.github.com/users/cebe/repos","events_url":"https://api.github.com/users/cebe/events{/privacy}","received_events_url":"https://api.github.com/users/cebe/received_events","type":"User","site_admin":false},"open_issues":164,"closed_issues":24,"state":"open","created_at":"2016-07-11T13:42:09Z","updated_at":"2017-02-12T23:15:03Z","due_on":"2017-03-26T07:00:00Z","closed_at":null},"comments":10,"created_at":"2014-10-03T18:31:57Z","updated_at":"2017-02-13T19:18:49Z","closed_at":null,"body":"I have the following tables and fields (simplified a bit here) and associated AR models\n\n```\ntbl_account\nid, plan_id, name\n\ntbl_user\nid, name\n\ntbl_plan\nid, name\n\ntbl_account_user\nid, account_id, user_id, type\n```\n\nThe FK's are obvious.\n\nBasically ...\nAn account has one plan.\nUser and account is many to many.\nA plan can belong to many accounts and therefore be related to many users.\n\nI want to get all users that are related to a plan so I have setup the following relations in the Plan model ...\n\n```\npublic function getAccounts()\n{\n   return $this->hasMany(Account::className(), ['plan_id' => 'id']);\n}\npublic function getAccountUsers()\n{\n    return $this->hasMany(AccountUser::className(), ['account_id' => 'id'])->via('accounts');\n}\npublic function getUsers()\n{\n    return $this->hasMany(User::className(), ['id' => 'user_id'])->via('accountUsers');\n}\n```\n\nIf I call $planModel->accountUsers I get the expected models but calling $planModel->users returns no results.\n\nI have done some digging and have found the problem is that the user models are correctly fetched from the db but they are indexed by the account_id rather than the user_id so are not found when populating the relation.\n\nIs this expected behavior?\nShould I be doing something differently in this scenario?\n\nI know this is probably a rare case and I'm most likely doing something stupid but I couldn't find any docs or examples anywhere for setting up these types of multi-level relations so it would be very useful to know where I'm going wrong.\n"},"comment":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/comments/279493175","html_url":"https://github.com/yiisoft/yii2/issues/5341#issuecomment-279493175","issue_url":"https://api.github.com/repos/yiisoft/yii2/issues/5341","id":279493175,"user":{"login":"cebe","id":189796,"avatar_url":"https://avatars.githubusercontent.com/u/189796?v=3","gravatar_id":"","url":"https://api.github.com/users/cebe","html_url":"https://github.com/cebe","followers_url":"https://api.github.com/users/cebe/followers","following_url":"https://api.github.com/users/cebe/following{/other_user}","gists_url":"https://api.github.com/users/cebe/gists{/gist_id}","starred_url":"https://api.github.com/users/cebe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cebe/subscriptions","organizations_url":"https://api.github.com/users/cebe/orgs","repos_url":"https://api.github.com/users/cebe/repos","events_url":"https://api.github.com/users/cebe/events{/privacy}","received_events_url":"https://api.github.com/users/cebe/received_events","type":"User","site_admin":false},"created_at":"2017-02-13T19:18:49Z","updated_at":"2017-02-13T19:18:49Z","body":"As the label says: \"complexity:hard\" ;) so it is not likely to be fixed very soon. This needs a rewrite of the bucket matching algorithm in eager loading implementation of AR which is quite complex already and might get even more complex when fixing this."}}
{"actor":{"display_login":"cebe"}}