mysql - CodeIgniter- active record insert if new or update on duplicate -
is possible active record query in codeigniter update existing record if 1 exists or insert if doesnt, given key?
i understand done first querying find existing record, i'm seeking efficient approach.
basically looking might insert ... on duplicate key update
- provided using mysql , id unique key on table.
you'd have manually construct query , pass $this->db->query()
function instead of built in active record helper functions of db driver.
example:
$sql = 'insert menu_sub (id, name, desc, misc) values (?, ?, ?, ?) on duplicate key update name=values(name), desc=values(desc), misc=values(misc)'; $query = $this->db->query($sql, array( $id, $this->validation->name, $this->validation->desc, $this->validation->misc ));
Comments
Post a Comment