phpcodeigniterreplacequery-builder

Difference between $this->db->replace() $this->db->update()


I don't get the difference between query builder's Replace and Update. Especially the documentation for Replace...

This method executes a REPLACE statement, which is basically the SQL standard for (optional) DELETE + INSERT, using PRIMARY and UNIQUE keys as the determining factor.

...but I see no indication of using a PK in the example. Am I missing some fundamental knowledge here? (I understand Update just fine).

Replace

$data = array(
    'title' => 'My title',
    'name'  => 'My Name',
    'date'  => 'My date'
);

$this->db->replace('table', $data);

Update

$data = array(
    'title' => $title,
    'name' => $name,
    'date' => $date
);

$this->db->where('id', $id);
$this->db->update('mytable', $data);

Thanks.


Solution

  • REPLACE

    It's like insert. but if the primary key being inserted is the same one as another one, the old one will be deleted and the new one will be inserted.

    UPDATE

    Updates the current row that you tried to update.