$dao = CRM_Core_DAO::executeQuery($sql);
// build insert query, note that currently we build cache for 500 (self::CACHE_SIZE) contact records at a time, hence below approach
- $insertValues = array();
+ $rows = [];
while ($dao->fetch()) {
- $insertValues[] = "('civicrm_contact', {$dao->contact_id}, {$dao->contact_id}, '{$cacheKey}', '" . CRM_Core_DAO::escapeString($dao->sort_name) . "')";
+ $rows[] = [
+ 'entity_table' => 'civicrm_contact',
+ 'entity_id1' => $dao->contact_id,
+ 'entity_id2' => $dao->contact_id,
+ 'data' => $dao->sort_name,
+ ];
}
- //update pre/next cache using single insert query
- if (!empty($insertValues)) {
- $sql = 'INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) VALUES
-' . implode(',', $insertValues);
-
- $result = CRM_Core_DAO::executeQuery($sql);
- }
+ Civi::service('prevnext')->fillWithArray($cacheKey, $rows);
}
/**
/**
* Store the results of a SQL query in the cache.
*
+ * @param string $cacheKey
* @param string $sql
* A SQL query. The query *MUST* be a SELECT statement which yields
* the following columns (in order): entity_table, entity_id1, entity_id2, cacheKey, data
*/
public function fillWithSql($cacheKey, $sql);
+ /**
+ * Store the contents of an array in the cache.
+ *
+ * @param string $cacheKey
+ * @param array $rows
+ * A list of cache records. Each record should have keys:
+ * - entity_table
+ * - entity_id1
+ * - entity_id2
+ * - data
+ * @return bool
+ */
+ public function fillWithArray($cacheKey, $rows);
+
}
return TRUE;
}
+ public function fillWithArray($cacheKey, $rows) {
+ if (empty($rows)) {
+ return;
+ }
+
+ $insert = CRM_Utils_SQL_Insert::into('civicrm_prevnext_cache')
+ ->columns([
+ 'entity_table',
+ 'entity_id1',
+ 'entity_id2',
+ 'cacheKey',
+ 'data'
+ ]);
+
+ foreach ($rows as &$row) {
+ $insert->row($row + ['cacheKey' => $cacheKey]);
+ }
+
+ CRM_Core_DAO::executeQuery($insert->toSQL());
+ return TRUE;
+ }
+
}