Friday, May 13, 2016

db_merge combination of db_insert and db_update

In drupal 7, there is a something new for table insertion and update. That is called db_merge which combines db_insert and db_update in a single query for single or multiple row insert and update.

$query = db_merge('table_name')
                ->insertFields(array(
                    'field1' => value1,
                    'field2' => value2,
                    'field3' => value3,
                ))
                ->updateFields(array(
                    'field3' => value3,
                ))
                ->key(array('field2' => value2))
                ->execute();

No comments:

Post a Comment