Codeigniter加入搞乱'id'字段

时间:2013-12-03 00:23:06

标签: php mysql codeigniter join

我有一个codeigniter查询来从我的数据库中获取问题列表。我正在使用一些连接来获取类别名称和答案类型。它可以输出所有内容,但是当我尝试从“questions”表中输出行的id时,它显示为“2”,即answer_type_id。

也许我做错了,我很擅长加入。任何帮助表示赞赏:

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;

    }

更新===============

我为我的联接类型添加了“left”但问题仍然存在。问题id应该是2和3.但是当我打印我的数组返回时,它们都是2(这是answer_type_id)。

以下是更新的代码(仅左连接已更改)...

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;

    }

这是它返回的输出:

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 
        ) 
) 

5 个答案:

答案 0 :(得分:2)

您没有获得正确ID的原因是您正在选择*。 使用别名以便为它们指定单独的名称,您将访问它们 如你所愿。

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();
}

同样Codeigniter数据库类提供方法调用result_array() 这已经是你正在使用的循环的替代。所以用它代替 额外的循环代码。

答案 1 :(得分:0)

只需将其更改为:

'id' => $row->category_id

问题是id列不明确。如果您在具有相同名称的列的表中选择列,则还可以使用AS关键字重命名列。

例如:

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

答案 2 :(得分:0)

偶然的机会你(也许)只得到一行,所有的id匹配?

您应该指定您的联接类型,或者在查询中更具体,以便保留question.id值。

对于这个问题,您可以将联接指定为左联接,以保留问题表中的所有数据,并从类别和answer_types中找到它。

请参阅CodeIgniter ActiveRecord docs

答案 3 :(得分:0)

问题ID需要定义为自己的名称。要从其他表中提取所有数据,请跟进其他表的星号,如下所示......

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;

    }

答案 4 :(得分:0)

为您的id列使用别名,如上例所示。

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

现在您获取数据,因为只有一个id列。

这里有一篇文章:http://chrissilich.com/blog/codeigniter-active-record-aliasing-column-names-to-prevent-overwriting-especially-columns-named-id/