Dushan
Dushan

Reputation: 138

Codeigniter delete record where some select query?

In codeigniter I want to delete record from a table. But at the same time I want to remove a record from another table which having foreign key to that record. Is there any possible way to do this using codeigniter?

1st delete query

$this->db->where('id',$id);
$this->db->delete('category_info');

1st table

id | description | image

2nd table

id | category_id | parent_category

Upvotes: 0

Views: 582

Answers (1)

Ben Fortune
Ben Fortune

Reputation: 32118

Instead of having PHP doing the heavy work, you can do this straight from MYSQL with a trigger. This will trigger after a deletion.

CREATE TRIGGER delete_trigger_tbl_2 AFTER DELETE ON category_info
FOR EACH ROW
BEGIN
DELETE FROM tbl_2
    WHERE tbl_2.id = old.id; 
-- Or category_id, not clear in your question.
END

Upvotes: 2

Related Questions