0

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 table 和 table 中获取结果,question并且answer都有 column 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;

有没有办法解决这个问题?

4

1 回答 1

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

结果行现在应该包含一个question和一个answer索引。

于 2012-11-21T21:06:01.723 回答