我有一个表2具有am:m关系的表,我可以想要的是,当我从其中一个表中删除一行时,我希望删除连接表中的行,我的sql是跟随,
表1
CREATE TABLE IF NOT EXISTS `job_feed` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`body` text NOT NULL,
`date_posted` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
表2
CREATE TABLE IF NOT EXISTS `job_feed_has_employer_details` (
`job_feed_id` int(11) NOT NULL,
`employer_details_id` int(11) NOT NULL,
PRIMARY KEY (`job_feed_id`,`employer_details_id`),
KEY `fk_job_feed_has_employer_details_job_feed1` (`job_feed_id`),
KEY `fk_job_feed_has_employer_details_employer_details1` (`employer_details_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
所以我想要做的是,如果从table1中删除了一行并且id为1,我希望表中的行也将该想法作为关系的一部分。
我想这样做是为了保持codeigniters活跃的记录类我目前有这个,
public function deleteJobFeed($feed_id)
{
$this->db->where('id', $feed_id)
->delete('job_feed');
return $feed_id;
}
答案 0 :(得分:0)
尝试这样的事情(我可能有一些错误的名字):
public function deleteJobFeed($feed_id)
{
//Delete Job Feed
$this->db->where('id', $feed_id)
->delete('job_feed');
//Get Related Employer information ID from related table
$query = $this>db->where('job_feed_id', $feed_id)
->get('job_feed_has_employer_details');
if ($query->num_rows() > 0)
{
$row = $query->row();
$employer_details_id = $row->employer_details_id;
}
//Now you have the employer details id.
//You can now delete the row you just got, and then use the id to find the row in the employer details table and delete that.
$this>db->where('job_feed_id', $feed_id)
->delete('job_feed_has_employer_details');
//I'm assuming your third table is called "employer details". You said you had a many to many relationship.
$this>db->where('employer_details_id', $employer_details_id)
->delete('employer_details');
return $feed_id;
}
另外,如果你还没有调查过,我建议你http://www.overzealous.com/dmz/
它使处理关系变得容易。希望这会有所帮助。