简体   繁体   中英

Codeigniter Join messing up 'id' field

I have a codeigniter query to get a list of questions from my database. I'm using some joins to get the category name and the answer type. Its working good to output everything, but when i try and output the id of the row from the "questions" table, it displays as "2" which is the answer_type_id.

Maybe I'm doing this wrong, i'm fairly new to joins. Any help is appreciated:

function get_questions($category_id) {
        $this->db->select('*');
        $this->db->from('questions');
        $this->db->where('category_id', $category_id);
        $this->db->join('categories', 'questions.category_id = categories.id');
        $this->db->join('answer_type', 'questions.answer_type_id = answer_type.id');
        $this->db->order_by('priority', 'desc');
        $query = $this->db->get();
        $data = array();

        foreach ($query->result() as $row) {
        $data[] = array(
            'id' => $row->id,
            'category' => $row->category,
            'type' => $row->type,
            'question' => $row->question,
            'answer' => $row->answer_type,
            'priority' => $row->priority,
        );
        }
        return $data;

    }

Update===============

I have added "left" for my join type but the problem persists. The question id's should be 2 and 3. But when i print my array returned, they are both 2 (which is the answer_type_id).

Here is the updated code (only left join changed)...

function get_questions($category_id) {
        $this->db->select('*');
        $this->db->from('questions');
        $this->db->where('category_id', $category_id);
        $this->db->join('categories', 'questions.category_id = categories.id', 'left');
        $this->db->join('answer_type', 'questions.answer_type_id = answer_type.id', 'left');
        $this->db->order_by('priority', 'desc');
        $query = $this->db->get();
        $data = array();

        foreach ($query->result() as $row) {
        $data[] = array(
            'id' => $row->id,
            'category' => $row->category,
            'type' => $row->type,
            'question' => $row->question,
            'answer' => $row->answer_type,
            'priority' => $row->priority,
        );
        }
        return $data;

    }

And here is the output it returns:

Array ( 
        [0] => Array ( 
                    [id] => 2 
                    [category] => Herbs 
                    [type] => 2 
                    [question] => What Type of Vehicle do You own? 
                    [answer] => Drop down list [priority] => 0 
        ) 
        [1] => Array ( 
                    [id] => 2 
                    [category] => Herbs 
                    [type] => 3 
                    [question] => What is Your Favorite Herb 
                    [answer] => Drop down list [priority] => 0 
        ) 
) 

The reason you are not getting the proper ids is you are selecting * . Use aliases so that give them separate names and you will be access them as you want like this.

function get_questions($category_id) {
    $this->db->select('questions.*');
    $this->db->select('answer_type.answer_type_id');
    $this->db->select('answer_type.other_column');
    $this->db->from('questions');
    $this->db->where('category_id', $category_id);
    $this->db->join('categories', 'questions.category_id = categories.id', 'left');
    $this->db->join('answer_type', 'questions.answer_type_id = answer_type.id', 'left');
    $this->db->order_by('priority', 'desc');
    return $this->db->get()->result_array();
}

Also Codeigniter database class provide a method call result_array() Which is already alternate of the loop you are using. So use it instead of extra loop code.

Just change it to this:

'id' => $row->category_id

The problem is that the id column is ambiguous. In cases where your selecting columns across tables with columns that have the same name, you can also use the AS keyword to rename a column.

For example:

$this->db->select("questions.*, categories.id AS cat_id, answer_type.id AS ans_id");

By chance are you (maybe) only getting one row as a result, where all the ids match?

You should specify your join type, or be more specific in your query, so that you keep the question.id value.

For this question, you could specify the join as a left join, to keep all the data from questions table and tack on from categories and answer_types where its found.

see CodeIgniter ActiveRecord docs

The questions id needs to be defined as its own name. To pull ALL the data from the other tables, follow up with asterisks for the other tables, like so...

function get_questions($category_id) {
        $this->db->select('questions.id as questions_id, questions.*, categories.*, answer_type.*');
        $this->db->from('questions');
        $this->db->where('category_id', $category_id);
        $this->db->join('categories', 'questions.category_id = categories.id', 'left');
        $this->db->join('answer_type', 'questions.answer_type_id = answer_type.id', 'left');
        $this->db->order_by('priority', 'desc');
        $query = $this->db->get();
        $data = array();

        foreach ($query->result() as $row) {
        $data[] = array(
            'id' => $row->questions_id,
            'category' => $row->category,
            'type' => $row->type,
            'question' => $row->question,
            'answer' => $row->answer_type,
            'priority' => $row->priority,
        );
        }
        return $data;

    }

Use alias for your id columns like the above example.

...
$this->db->select("questions.*, categories.id AS cat_id, answer_type.id AS ans_id");
...
...

Now you get your data because only have one id column.

Here you have an article: http://chrissilich.com/blog/codeigniter-active-record-aliasing-column-names-to-prevent-overwriting-especially-columns-named-id/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM