og2list using mysql_next_id() instead of db_next_id()
| Project: | Organic groups list manager |
| Version: | HEAD |
| Component: | Code |
| Category: | bug report |
| Priority: | normal |
| Assigned: | damien_vancouver |
| Status: | closed |
When creating a new message in og2list_outgoing_content, og2list is using PHP's mysql_insert_id() to find the identity value of og2list_outgoing_content.mid. When promoting og2list to one of my sites I discovered that this function was returning a value of 0 instead of the identity column.
db_query("INSERT INTO {og2list_outgoing_content} (to_name, to_address, from_name, from_address, subject, msgid, body, is_node, id) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d)", $edit['ogs'][$og]['name'], $edit['ogs'][$og]['address'], $edit['name'], $edit['mail'], $subject, $msg_id, "<html><body>$body</body></html>", $edit['cid'] ? 0 : 1, $edit['cid'] ? $edit['cid'] : $edit['nid']);
$mid = mysql_insert_id();The proper Drupal way to do this with the Drupal database layer is to use Drupal's db_next_id() function (http://api.drupal.org/api/5/function/db_next_id), using a sequence and removing the auto_increment property on og2list_outgoing_content.mid.
Finally, there may be existing messages in here, and the correct value in sequences for og2list_outgoing_content_mid must be set to the maximum value of mid for messages already in the system.
The attached patch implements this change, and got things working properly again on the site I was working with.
og2list.install:
- added og2list_update_9() to fix the table schema and insert the correct sequences row.
og2list.module:
- changed og2list_send_mail() to use db_next_id instead:
db_query("INSERT INTO {og2list_outgoing_content} (to_name, to_address, from_name, from_address, subject, msgid, body, is_node, id) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d)", $edit['ogs'][$og]['name'], $edit['ogs'][$og]['address'], $edit['name'], $edit['mail'], $subject, $msg_id, "<html><body>$body</body></html>", $edit['cid'] ? 0 : 1, $edit['cid'] ? $edit['cid'] : $edit['nid']);
$mid = db_next_id("{og2list_outgoing_content}_mid");| Attachment | Size |
|---|---|
| og2list_db_next_id_fix.patch | 2.13 KB |

#1
Oops, the last version was not quite right.
This version of the patch properly inserts the value of the new mid in og2list_outgoing_content as well.
#2
Applied, please mind the coding conventions (space after ",", no space after ").
#3