Skip to content

Instantly share code, notes, and snippets.

@futurefirst
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save futurefirst/9a6b39ddfa34eb01a417 to your computer and use it in GitHub Desktop.
Save futurefirst/9a6b39ddfa34eb01a417 to your computer and use it in GitHub Desktop.
How to get the new record ID, when adding a record to a multi-value custom field using the API?
<?php
/**
* CustomValue.Insert API specification (optional)
* This is used for documentation and validation.
*
* @param array $spec description of fields supported by this API call
* @return void
* @see http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards
*/
function _civicrm_api3_custom_value_insert_spec(&$spec) {
$spec['entity_id'] = array(
'title' => 'Entity ID',
'type' => CRM_Utils_Type::T_INT,
'api.required' => 1,
);
}
/**
* CustomValue.Insert API
*
* @param array $params
* @return array API result descriptor
* @see civicrm_api3_create_success
* @see civicrm_api3_create_error
* @throws API_Exception
*/
function civicrm_api3_custom_value_insert($params) {
$entityId = $params['entity_id'];
foreach ($params as $cfKey => $cfValue) {
if (strpos($cfKey, 'custom_') !== 0) {
continue;
}
// Details of requested custom field and its group
$dummy = explode('_', $cfKey);
$cfId = $dummy[1];
$cf = civicrm_api3('CustomField', 'getsingle', array('id' => $cfId));
$cfColumn = $cf['column_name'];
$cgId = $cf['custom_group_id'];
$cg = civicrm_api3('CustomGroup', 'getsingle', array('id' => $cgId));
$cgTable = $cg['table_name'];
break;
}
// Validate params
if (array_key_exists('id', $params)) {
throw new API_Exception('This API is only for adding new records at the moment');
}
if (!$cg['is_multiple']) {
throw new API_Exception('This API is for use with multiple-record custom groups only');
}
if (empty($entityId) || empty($cfId)) {
throw new API_Exception('Entity ID and custom field must be specified');
}
if (empty($cfColumn) || empty($cgTable)) {
throw new API_Exception('Error looking up details of custom field or custom group');
}
// Add the record, get its ID (what we're really interested in)
// Data saved through core APIs is normally escaped in the DB, do that too
$dao = CRM_Core_DAO::executeQuery("
INSERT INTO `%0` (`entity_id`, `%1`) VALUES (%2, %3)
", array(
array($cgTable, 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
array($cfColumn, 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
array($entityId, 'Int'),
array(htmlentities($cfValue), 'String'),
));
$cvId = CRM_Core_DAO::singleValueQuery("SELECT LAST_INSERT_ID()");
// Replicate the hook params that come from a CustomValue.create
$hookParams = array(array(
'entity_id' => $entityId,
'value' => $cfValue,
'type' => $cf['data_type'],
'custom_field_id' => $cfId,
'custom_group_id' => $cgId,
'table_name' => $cgTable,
'column_name' => $cfColumn,
'is_multiple' => $cg['is_multiple'],
));
CRM_Utils_Hook::custom('create', $cgId, $entityId, $hookParams);
// Replicate the output from something more like a create/setvalue
$returnValues = array($cvId => array(
'id' => $cvId,
'entity_id' => $entityId,
$cfKey => $cfValue,
$cfKey . '_' . $cvId => $cfValue,
));
return civicrm_api3_create_success($returnValues, $params, 'CustomValue', 'Insert');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment