A merge
query attempts to automatically determine whether a record should be inserted or updated depending on whether a record already exists in a database matching a unique key for the table. Because many databases do not implement merges in a standard method, the DBTNG layer delegates much of the implementation to the database driver.
To create a merge
statement, you begin by calling db_merge
, which has the following signature:
db_merge($table, array $options = array())
Just like the other methods we have looked at so far, you begin the query by passing in the name of the table that you are working with. After you create the query, you must tell Drupal how to determine whether a record already exists in the database or not. This is done by calling the key
method with an associative array with the name of the field or fields that should be checked as well as the existing values that need to be checked. Finally, you need to specify the values for each field that need...