OK, so I read about drupal_write_record
, which apparently serves as a function which does an insert_or_update.
However from what I've read, it doesn't know by itself whether to do the insert or the update, but this is determined by the existence (or not) of the 3rd parameter: $primary_keys
which defaults to array()
. Is this true? Do I have to select the record first, see if exists, and then pass or not the 3rd param to drupal_write_record
?
I'm asking this, because how I normally do an insert or update function is to do a select based on the primary key(s), and if the row exists, then I have to do an update, if not, an insert. Is this not the way that drupal_write_record
works?
Using Drupal 7.0 by the way.
According to the documentation:
$primary_keys: To indicate that this is a new record to be inserted, omit this argument. If this is an update, this argument specifies the primary keys' field names. If there is only 1 field in the key, you may pass in a string; if there are multiple fields in the key, pass in an array.
Source: drupal_write_record API documentation
If the third parameter-- $primary_keys
-- is an array with the field names for primary keys, then it will attempt to update an exist record. If you leave out a value $primary_keys
, it will default to an empty array which means that it will insert a new record with its own unique ID.