我可以在Codeigniter中确定query_result数组的键吗?

时间:2012-10-08 05:16:55

标签: codeigniter

以下get_poll模型中的Poll函数中的

class Poll_model extends CI_Model 
{
    public function get_poll($parameter) {
        $this->db->select('question.id, question.title, question.question, answer.answer')->from('answer')->join('question', 'answer.question_id = question_id')->where('question.id',$parameter);
        $query = $this->db->get();
        return $query->result_array();
}

因为我使用join来获取2个表的结果,而表questionanswer都有列content,所以在result_array中,结构如下:

Array ( [id] => 1 [title] => favourate character [content] => Green ) 1

只有answer content,我认为question content是覆盖的,因为它们都有相同的列'内容'。表结构如下所示:

CREATE TABLE `answer` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `question_id` int(11) unsigned NOT NULL,
  `content` text NOT NULL,
  PRIMARY KEY (`id`),
  KEY `question_id` (`question_id`),
  CONSTRAINT `answer_ibfk_1` FOREIGN KEY (`question_id`) REFERENCES `question` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;


CREATE TABLE `question` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(128) NOT NULL DEFAULT '',
  `content` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;

有什么方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

只需重命名->select()调用中的列,如下所示:

$this->db
    ->select('
        question.id id, 
        question.title title, 
        question.content question, 
        answer.content answer')
    ->from('answer')
    ->join('question', 'answer.question_id = question_id')
    ->where('question.id', $parameter);

结果行现在应包含questionanswer索引。