Using Join in CodeIgniter – Active Record Delete

Written by Ed Genochio on December 13, 2011

You might try this, using CodeIgniter’s Active Record class:

$this->db->join("table2", "table1.thing_id = table2.id")
  ->where("table2.otherthing_id", $id)
  ->delete("table1");

 

This doesn’t work, as CodeIgniter ignores joins when doing Active Record deletes.

Instead, you’ll have to write out the SQL ‘manually’ – something like this:

$sql = "DELETE t1 FROM table1 t1
  JOIN table2 t2 ON t1.thing_id = t2.id
  WHERE t2.otherthing_id = ?";
$this->db->query($sql, array($id));

We love solving problems. If you want to find out more about what we do, get in touch with us here.