我有一个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
)
)
您没有获得正确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()
这已经是你正在使用的循环的替代。所以用它代替额外的循环代码。
只需将其更改为:
'id' => $row->category_id
问题是id
列不明确。如果在具有相同名称列的表中选择列,也可以使用AS
关键字重命名列。
例如:
$this->db->select("questions.*, categories.id AS cat_id, answer_type.id AS ans_id");
你(可能)只得到一行,所有id都匹配吗?
您应该指定联接类型,或者在查询中更加具体,以便保留question.id值。
对于这个问题,您可以将联接指定为左联接,以保留问题表中的所有数据,并从其所在的类别和answer_types中添加。
请参阅CodeIgniter ActiveRecord文档
问题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;
}
像上面的例子一样为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/