Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

codeigniter

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.

like image 898
user3442612 Avatar asked Aug 22 '16 06:08

user3442612


1 Answers

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.

like image 76
Lorenzo Avatar answered Nov 12 '22 22:11

Lorenzo