{"action":"created","issue":{"url":"https://api.github.com/repos/jamierumbelow/codeigniter-base-model/issues/220","repository_url":"https://api.github.com/repos/jamierumbelow/codeigniter-base-model","labels_url":"https://api.github.com/repos/jamierumbelow/codeigniter-base-model/issues/220/labels{/name}","comments_url":"https://api.github.com/repos/jamierumbelow/codeigniter-base-model/issues/220/comments","events_url":"https://api.github.com/repos/jamierumbelow/codeigniter-base-model/issues/220/events","html_url":"https://github.com/jamierumbelow/codeigniter-base-model/issues/220","id":137889338,"number":220,"title":"Custom \"with()\" method that has two joins, how to?","user":{"login":"paulcanning","id":1275739,"avatar_url":"https://avatars.githubusercontent.com/u/1275739?v=3","gravatar_id":"","url":"https://api.github.com/users/paulcanning","html_url":"https://github.com/paulcanning","followers_url":"https://api.github.com/users/paulcanning/followers","following_url":"https://api.github.com/users/paulcanning/following{/other_user}","gists_url":"https://api.github.com/users/paulcanning/gists{/gist_id}","starred_url":"https://api.github.com/users/paulcanning/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paulcanning/subscriptions","organizations_url":"https://api.github.com/users/paulcanning/orgs","repos_url":"https://api.github.com/users/paulcanning/repos","events_url":"https://api.github.com/users/paulcanning/events{/privacy}","received_events_url":"https://api.github.com/users/paulcanning/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2016-03-02T14:33:45Z","updated_at":"2016-03-03T16:59:16Z","closed_at":null,"body":"I am struggling to write a custom \"with()\" method that involves two joins.\r\n\r\nI'll try to breakdown my tables:\r\n\r\nUsers - generic table, users can be joined to other tables via lookup tables.\r\nResellers - entity that describes a reseller, has an owner_id which is a foreign key to users.id\r\nAgents - lookup table to connect a (different) user to a reseller, as an agent (sales person). Can be multiple per reseller.\r\n\r\nSo, typically, looking up a reseller, I can get the owner (users) details by using the $belongs_to relationship and using \"with('users')\" in my call, before get_all() eg $this->resellers->with('users')->get_all()\r\n\r\nThis works great!\r\n\r\nNow, the tricky part.\r\n\r\nI want to get the above, but with the agent(s) details. eg $this->reseller->with('users')->with_agents()->get_all()\r\n\r\nI tried the following:\r\n\r\n```\r\npublic function with_agents() {\r\n    $this->db->select('users.first_name AS agent_name');\r\n    $this->db->join('agents_resellers', 'users_resellers.reseller_id = resellers.id', 'LEFT');\r\n    $this->db->join('users u', 'u.id = agents_resellers.user_id', 'LEFT');\r\n\r\n    return $this\r\n}\r\n```\r\n\r\nBut I get an error: \r\n\r\n> Notice 0 - Undefined property: stdClass::$owner_id ... application\\core\\MY_Model.php on line 473\r\n\r\nSooo, how can I write a custom \"with()\" method, that has multiple joins, and where it is fetching one or more results? (not just one result like the user join)"},"comment":{"url":"https://api.github.com/repos/jamierumbelow/codeigniter-base-model/issues/comments/191859052","html_url":"https://github.com/jamierumbelow/codeigniter-base-model/issues/220#issuecomment-191859052","issue_url":"https://api.github.com/repos/jamierumbelow/codeigniter-base-model/issues/220","id":191859052,"user":{"login":"paulcanning","id":1275739,"avatar_url":"https://avatars.githubusercontent.com/u/1275739?v=3","gravatar_id":"","url":"https://api.github.com/users/paulcanning","html_url":"https://github.com/paulcanning","followers_url":"https://api.github.com/users/paulcanning/followers","following_url":"https://api.github.com/users/paulcanning/following{/other_user}","gists_url":"https://api.github.com/users/paulcanning/gists{/gist_id}","starred_url":"https://api.github.com/users/paulcanning/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paulcanning/subscriptions","organizations_url":"https://api.github.com/users/paulcanning/orgs","repos_url":"https://api.github.com/users/paulcanning/repos","events_url":"https://api.github.com/users/paulcanning/events{/privacy}","received_events_url":"https://api.github.com/users/paulcanning/received_events","type":"User","site_admin":false},"created_at":"2016-03-03T16:59:16Z","updated_at":"2016-03-03T16:59:16Z","body":"@michail1982 care to provide an example? :)"}}