4

Within a new extension, in a form's postProcess function, I am inserting into a non-Civi table (separate mysql database/table). What is the proper way to execute a MySQL insert statement and retrieve the new id from that insert using CRM_Core_DAO? I use executeQuery for selects, and can use it to run the insert as well, but not sure that's the best way and not sure how to get the id back.

5

You could do something like CRM_Core_DAO::executeQuery("SELECT LAST_INSERT_ID()");. If your needs are simple that seems like the easiest.

Probably to do it "right", you would use civix generate:entity. See https://docs.civicrm.org/dev/en/latest/extensions/civix/#generate-entity

Then you could do things in an object-y way, like:

$myclass = new MyClass(); $myclass->field1 = 'foo'; $myclass->field2 = 'bar'; $myclass->save(); // Then the inserted id is automatically in $myclass->id (assuming your auto_increment field is called 'id'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.