Tuesday, June 11, 2013

How to update a JOINed tables using Codeigniter's Active Record?

Codeigniter active record doesn't allow to update a joined tables.
After trying various method and searching the solution, I have found the following solution which does the exactly same thing i.e. update the multiple join tables. By using following method, you can update multiple table using codeigniter active record.
$this->db->set('a.firstname', 'Pekka');
$this->db->set('a.lastname', 'Kuronen');
$this->db->set('b.companyname', 'Suomi Oy');
$this->db->set('b.companyaddress', 'Mannerheimtie 123, Helsinki Suomi');

$this->db->where('a.id', 1);
$this->db->where('a.id = b.id');
$this->db->update('table as a, table2 as b');


7 comments:

  1. This method will not work with CI3. I know this because I used it often in CI2. I do not have a replacement yet except maybe update_batch()

    ReplyDelete
  2. Is it work for more than two table ?

    ReplyDelete
    Replies
    1. It should work with many tables but you should correctly use join.

      Delete
  3. Can I use '$data' array for '$this->db->set()' method?

    ReplyDelete
  4. how to use in controller and model? i'm confused, update data in 3 tables.
    can you help me please?

    ReplyDelete