Get Insert Id
Get the atuomatically generated ID of the item that was just inserted into the database.
The function:
function getInsertId($conn=NULL) {
if(!is_resource($conn)) { $conn = $this->conn; }
return mysql_insert_id($conn);
}
To use
$newId = $modx->db->getInsertId();
Example
$email = checkMail($_POST['email']);
$pass = makePass();
$sql = "INSERT INTO ".$modx->getFullTableName('web_users')." (username,password) VALUES ('".$email."', '".$pass."')";
$rs = $modx->db->query($sql);
$id = $modx->db->getInsertId();
$sql = "INSERT INTO ".$modx->getFullTableName('web_user_attributes')." (internalKey,email) VALUES (".$id.",'".$email."')";
$rs = $modx->db->query($sql);
In this case, we've used a simple registration form to get the email of a new member. First the returned POST value is checked through a function. A password is automatically generated with another function. These values are used as the username and password, and inserted into the web_users table. The automatically generated ID for this new member is retrieved with the getInsertId function. The new member's ID is now used to insert the user's email into the web_user_attributes table.
This same value can then be used to insert the new member into a desired web group. This would be useful for registering a member to receive a newsletter, for example.