I want to achieve same thing like the SQL code below (it works)shown in ORM in Codeigniter:
SELECT question.`id`,`title`,`question`,`answer` FROM answer LEFT JOIN question ON answer.question_id = question.id WHERE question.`id` = 1
I made the following code:
$this->db->select('question.id, question.title, question.question, answer.answer')->from('answer')->join('question', 'answer.question_id = question_id')->where('question.id',1);
$query = $this->db->get();
it doesn’t work, instead of select question.id = 1, it get all the answer, it seems like the where clause doesn’t work at all
I provide the table structure below
CREATE TABLE `question` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(128) NOT NULL DEFAULT '',
`question` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
CREATE TABLE `answer` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`question_id` int(11) unsigned NOT NULL,
`answer` 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;
You should know active record is not ORM. If you want to get this with active record here is how you can do it. For detail read the user guide of Codeigniter