如何使用CodeIgniter中的Activerecord将其他表插入表中? [英] How to insert into a table join other table using Activerecord in CodeIgniter?

查看:103
本文介绍了如何使用CodeIgniter中的Activerecord将其他表插入表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CodeIgniter和ORM的新手,希望你们能为我提供帮助.

I'm new to CodeIgniter and ORM, I hope you guys can help me with this.

问题表:

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之外,有人可以告诉我如何实现相同的目标吗?

Anyone can tell me how to achieve the same thing but using CodeIgniter Activerecord?

推荐答案

不要这样做,而是使用主键(id)直接插入基表中.

Don't do that, instead use the primary key (id) to insert directly into the base table.

这篇关于如何使用CodeIgniter中的Activerecord将其他表插入表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆