Created
March 3, 2015 14:32
-
-
Save jonhattan/5a541db06a614d1f585e to your computer and use it in GitHub Desktop.
db_merge() example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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