I want to do an insert of row in a table, like this:
$this->db->update_batch($this->table_name, $update, 'image_id');
if ($this->db->affected_rows() === 0) {
$this->db->insert_batch($this->table_name, $update);
}
but if it exists I don't want to do anything. But this code above inserts another row because no row is affected
I guess I could do a INSERT IGNORE INTO but I would prefer using CodeIgniters upate_batch and insert_batch.
$update
- variable is something like
$update = array(
array('id' => 1, 'name' => 'Gustav'),
array('id' => 2, 'name' => 'Peter'),
array('id' => 3, 'name' => 'Lisa')
)
UPDATE
By the answers I get that I wasn't clear enough in what I wanted. (and I didn't think I was clear enough what I wanted so here is my updated question which I hope is clearer)
//This will insert Gustav, Peter and Lisa
$update = array(
array('image_id' => 1, 'name' => 'Gustav'),
array('image_id' => 2, 'name' => 'Peter'),
array('image_id' => 3, 'name' => 'Lisa')
)
$this->db->insert_batch($update);
//If I do this it would insert Party Gustav, Peter and Lisa.
$update = array(
array('image_id' => 1, 'name' => 'Party Gustav'),
array('image_id' => 2, 'name' => 'Peter'),
array('image_id' => 3, 'name' => 'Lisa')
)
$this->db->insert_batch($update);
//Above will create 6 rows
BUT what I want to happen IS
$update = array(
array('image_id' => 1, 'name' => 'Gustav'),
array('image_id' => 2, 'name' => 'Peter'),
array('image_id' => 3, 'name' => 'Lisa')
)
$this->db->insert_batch($update);
$update = array(
array('image_id' => 1, 'name' => 'Party Gustav'),
array('image_id' => 2, 'name' => 'Peter'),
array('image_id' => 3, 'name' => 'Lisa')
)
//Insert batch but when existing image_ids just change the name
//from Gustav to Party Gustav (in this case)
$this->db->insert_batch($update);
//Above will create 3 rows
I guess it will equal to something like on key duplicate
for insert_batch
.