Ensures that e.g. an array of integer fields will be returned as integers and not an array of strings
*
* @see \Civi\Api4\Utils\FormattingUtil::formatOutputValues
* @param string $value
+ * @param string $dataType
* @return string|array
*/
- public function formatOutputValue($value) {
+ public function formatOutputValue($value, &$dataType) {
+ // Count is always an integer
+ $dataType = 'Integer';
return (int) $value;
}
*
* @see \Civi\Api4\Utils\FormattingUtil::formatOutputValues
* @param string $value
+ * @param string $dataType
* @return string|array
*/
- public function formatOutputValue($value) {
+ public function formatOutputValue($value, &$dataType) {
$exprArgs = $this->getArgs();
+ // By default, values are split into an array and formatted according to the field's dataType
if (!$exprArgs[2]['prefix']) {
$value = explode(\CRM_Core_DAO::VALUE_SEPARATOR, $value);
}
+ // If using custom separator, unset $dataType to preserve raw string
+ else {
+ $dataType = NULL;
+ }
return $value;
}
$fieldName = \CRM_Utils_Array::first($fieldExpr->getFields());
$field = $fieldName && isset($fields[$fieldName]) ? $fields[$fieldName] : NULL;
$dataType = $field['data_type'] ?? ($fieldName == 'id' ? 'Integer' : NULL);
- // If Sql Function e.g. GROUP_CONCAT or COUNT wants to do its own formatting, apply and skip dataType conversion
+ // If Sql Function e.g. GROUP_CONCAT or COUNT wants to do its own formatting, apply
if (method_exists($fieldExpr, 'formatOutputValue') && is_string($value)) {
- $result[$key] = $value = $fieldExpr->formatOutputValue($value);
- $dataType = NULL;
+ $result[$key] = $value = $fieldExpr->formatOutputValue($value, $dataType);
}
if (!$field) {
continue;
->addGroupBy('contact_id')
->addWhere('contact_id', '=', $cid)
->addSelect('GROUP_CONCAT(financial_type_id:name)')
+ ->addSelect('GROUP_CONCAT(financial_type_id)')
->addSelect('COUNT(*) AS count')
->execute()
->first();
$this->assertTrue(4 === $agg['count']);
$this->assertContains('Donation', $agg['GROUP_CONCAT:financial_type_id:name']);
+ foreach ($agg['GROUP_CONCAT:financial_type_id'] as $type) {
+ $this->assertTrue(is_int($type));
+ }
// Test GROUP_CONCAT with a CONCAT as well
$agg = Contribution::get(FALSE)