• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete cascade ?

#1
Question 
Hello,
I have an article table and a comment table. I hope that the comments of the article also disappear, how to do?

Thank you in advance
Reply

#2
Answer easily found:

http://lmgtfy.com/?q=mysql+on+delete+cascade
Reply

#3
(06-12-2017, 05:42 AM)tonny16 Wrote: Hello,
I have an article table and a comment table. I hope that the comments of the article also disappear, how to do?

Thank you in advance

PHP Code:
$data = array(
   'deleted' => 1
);

$this->db->where('article_id'$article_id);
$this->db->update('article'$data); 

i prefer the soft delete. After x-months you can delete the rows.

If you use foreign keys you only need to delete the article (on delete cascade)

http://www.mysqltutorial.org/mysql-foreign-key/


if not: delete first the comments then the article

PHP Code:
$this->db->where('article_id'$article_id);
$status $this->db->delete('comments');
...
$this->db->where('article_id'$article_id);
$this->db->delete('article');
.... 
Reply

#4
Thank you for your answers.

In mySQL I put this code:
PHP Code:
ALTER TABLE blog_commentaires
ADD CONSTRAINT fk_comments_article         
    FOREIGN KEY 
(id_article           
    REFERENCES article
(id)
 
   ON DELETE CASCADE


How to put it in Codeigniter?

Thank you in advance
Reply

#5
In CI you can run any SQL statement with the query function

$this->db->query("
ALTER TABLE blog_commentaires
ADD CONSTRAINT fk_comments_article
FOREIGN KEY (id_article)
REFERENCES article(id)
ON DELETE CASCADE;
");
On the package it said needs Windows 7 or better. So I installed Linux.
Reply

#6
Its all in the manual and much quicker to find then waiting for a reply in the forum

https://www.codeigniter.com/user_guide/d...mples.html
On the package it said needs Windows 7 or better. So I installed Linux.
Reply

#7
(06-15-2017, 09:27 AM)rtenny Wrote: Its all in the manual and much quicker to find then waiting for a reply in the forum

https://www.codeigniter.com/user_guide/d...mples.html

Thanks for all, I used an other solution 
PHP Code:
$this->db->select('*');
        
$this->db->from('article');
        
$this->db->join('blog_commentaires''blog_commentaires.id_article = article.id');
        
$this->db->where('statut'"0");
        
$this->db->order_by('blog_commentaires.date''DESC');
        
        
$this->comments $this->db->get()->result();

        return 
$this->comments
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.