function db_merge

You are here

7 database.inc db_merge($table, array $options = array())
8 database.inc db_merge($table, array $options = array())

Returns a new MergeQuery object for the active database.

Parameters

$table: The table into which to merge.

$options: An array of options to control how the query operates.

Return value

MergeQuery A new MergeQuery object for this connection.

Related topics

44 calls to db_merge()
actions_save in includes/actions.inc
Saves an action and its user-supplied parameter values to the database.
aggregator_refresh in modules/aggregator/aggregator.module
Checks a news feed for new items.
aggregator_save_category in modules/aggregator/aggregator.module
Adds/edits/deletes aggregator categories.
aggregator_save_item in modules/aggregator/aggregator.processor.inc
Adds/edits/deletes an aggregator item.
BlockInvalidRegionTestCase::testBlockInInvalidRegion in modules/block/block.test
Tests that blocks assigned to invalid regions work correctly.

... See full list

File

includes/database/database.inc, line 2444
Core systems for the database layer.

Code

function db_merge($table, array $options = array()) {
  if (empty($options['target']) || $options['target'] == 'slave') {
    $options['target'] = 'default';
  }
  return Database::getConnection($options['target'])->merge($table, $options);
}

Comments

I have spent some hours to figure out a db_merge command fails because of I use "group" as column name of the table. In this situation in MySQL, the column name needs to be quote by `group` but this seems not done in Drupal.

I got similar issues when I named a column "desc".

Hope this post saves someone's time.

Got the same trouble with "repeat". Here is a list of MySQL reserved words.

I found no examples here, though i though of adding mine

notice the key function, uid, mid are those table primary keys

<?php
  db_merge
('users_missions')
      ->
key(array('uid' => $user->uid, 'mid' => $mid))
      ->
fields(array(
           
'uid' => $user->uid,
           
'mid' => $mid,
           
'status' => 0,
           
'start_timestamp' => time()
      ))
      ->
execute();
?>

You may have some trouble using db_merge() if you're passing decimal values into keys() if your data is stored as FLOAT. From MYSQL manual:

"Using FLOAT might give you some unexpected problems because all calculations in MySQL are done with double precision."

If you do run into such an integrity contraint violation, try switching storage to type DOUBLE.

See:
http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html
http://dev.mysql.com/doc/refman/5.0/en/no-matching-rows.html