Welcome Guest, Not a member yet? Register   Sign In
issue with using join
#1

[eluser]kyleect[/eluser]
Here is a method from one of my models:

Code:
function get_newest($limit = NULL, $offset = NULL){
            if(isset($limit)){
                $this->db->limit($limit);
            }
            $this->db->select('*');
            $this->db->from('entries');
            $this->db->join('states', 'states.id = entries.state_id');
            $this->db->order_by('entries.timestamp', 'desc');
            return $this->db->get();
}

This is causing some real issues and I'm sure there is a simple solution. I have 2 tables. `entries` and `states`. Both have a field called `id`. Here is the code I'm using to display entries:

Code:
see below...

since both tables have `id` as a field, it's pull the data from states and breaking my app. I'm very unfamiliar with JOINs outside of CI so any help would be great. How can I specify that I want the `id` field from `entries, not `states`

If there is any information I've left out that will help, please let me know.

--

For some reason the CI forum tries to parse what I'm pasting in so the code won't display correctly. Also, I can't attach a file renamed view_name.txt as the mime type is incorrect. Wow, I think breaking my own fingers would be more enjoyable than this. OK. So I'm using a pastebin to get this posted. Ridiculous.

http://pastebin.com/m41d96e7e
#2

[eluser]Matthieu Fauveau[/eluser]
Hi,

Why not use $this->db->select('entries.id AS id'); ?
#3

[eluser]kyleect[/eluser]
That worked! Database work is by far my weakest point. Thanks for the help.
#4

[eluser]Matthieu Fauveau[/eluser]
No problem, glad to be of help from times to times Wink




Theme © iAndrew 2016 - Forum software by © MyBB