如何在CodeIgniter中使用Activerecord插入表中连接其他表?

时间:2012-10-06 11:52:00

标签: sql orm codeigniter-datamapper phpactiverecord

我是CodeIgniter和ORM的新手,我希望你们能帮助我。

问题表:

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 DEFAULT CHARSET=latin1;

答案表:

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 DEFAULT CHARSET=latin1;

等效的SQL是:

INSERT INTO answer(content, question_id) 
VALUES('Ironman', (select id 
                     from question 
                    where title ='favourite characters' 
                      and content = 'Who is your favourite characters in Avanger?'));

任何人都可以告诉我如何使用CodeIgniter Activerecord来实现相同的目标?

1 个答案:

答案 0 :(得分:0)

请勿使用主键(id)直接插入基表。