Skip to content

Instantly share code, notes, and snippets.

@jonhattan
Created March 3, 2015 14:32
Show Gist options
  • Save jonhattan/5a541db06a614d1f585e to your computer and use it in GitHub Desktop.
Save jonhattan/5a541db06a614d1f585e to your computer and use it in GitHub Desktop.
db_merge() example
<?php
// Track # of places sold for an event. The event is addressed by $entity_id and $date.
// $quantity is a positive number of places sold or negative, if places are cancelled.
db_merge('places_sold')
->key(
array(
'entity_id' => $entity_id,
'date' => $date->format(DATE_FORMAT_DATE),
)
)
->fields(array(
'quantity' => max(0, $quantity),
))
->expression('quantity', "GREATEST(0, quantity + $quantity)")
->execute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment