Welcome Guest, Not a member yet? Register   Sign In
[Solved] Going crazy with multiple ID joins
#1

[eluser]spmckee[/eluser]
I have spent the last hour pouring over posts related to the topic with nothing to show for it. Serious frustration.

I have three tables:

Code:
companies       |  projects        |  feeds
----------------------------------------------
id              |  id              |  id
company_name    |  company_id      |  project_id
company_status  |  project_name    |  feed_name
                |  project_status  |  feed_status


Here's my query:
Code:
function get_all()
    {
        $this->db
                ->join('projects', 'projects.id = feeds.project_id', 'left')
                ->join('companies', 'projects.company_id = companies.id', 'left')
                ->order_by('feeds.feed_name', 'DESC');
        
        return parent::get_all();
    }

And one of the Feed array results: (Main ID is incorrect)
Code:
[1] => stdClass Object
        (
            [id] => 32
            [project_id] => 9
            [feed_date_time] => 1313577000
            [feed_status] => 1
            [feed_name] => New Info
            [feed_layout] => 2
            [company_id] => 32
            [project_name] => Red Team
            [project_status] => 1
            [company_name] => Acme
            [company_status] => 1
        )

I understand that the "ID" field is being overwritten by the last id retrieved. This causes my feed id to get lost. I also suspect that I need to use some kind of aliasing or something but not sure. Any help? I really want to understand this.

Thanks for your time!
#2

[eluser]danmontgomery[/eluser]
You understand correctly. You need to either name the columns uniquely, or alias the result as you see fit (or only select the columns you want).

Code:
$this->db->select('*, companies.id AS company_id, projects.id AS project_id, feeds.id AS feed_id')
->join( // ...
#3

[eluser]spmckee[/eluser]
Awesome! I tweaked it a bit to retain my main id:

feeds.id AS feed_id -> feeds.id AS id

Code:
->select('*, companies.id AS company_id, projects.id AS project_id, feeds.id AS id')

Thanks a ton.




Theme © iAndrew 2016 - Forum software by © MyBB