mysql codeigniter活动记录m:m删除

发布于 2024-09-02 05:42:39 字数 1095 浏览 1 评论 0原文

我有一个具有 am:m 关系的表 2 表,我想要的是,当我从其中一个表中删除一行时,我希望连接表中的行也被删除,我的 sql 如下,

< strong>表 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;
    }

I have a table 2 tables that have a m:m relationship, what I can wanting is that when I delete a row from one of the tables I want the row in the joining table to be deleted as well, my sql is as follow,

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

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

So what I am wanting to do is, if the a row is deleted from table1 and has an id of 1 I want the row in table to that also has that idea as part of the relationship also.

I want to do this in keeping with codeigniters active record class I currently have this,

public function deleteJobFeed($feed_id)
    {
        $this->db->where('id', $feed_id)
                 ->delete('job_feed');

        return $feed_id;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聊慰 2024-09-09 05:42:39

尝试这样的事情(我可能弄错了一些名称):

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/

它使处理关系变得容易。希望这有帮助。

Try something like this (I may have gotten some of the names wrong):

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

Also, if you haven't looked into it, I recommend http://www.overzealous.com/dmz/

It makes handling relationships way easy. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文