vartec - thanks for your help. Your ideas gave me a good starting point. I've developed it further, extending Zend_Db_Table, adding a function that will suck the column metadata out of MySQL, and use this to set default values. I've posted a rough draft of it below. I haven't made any attempt to simplify it yet, and have only covered the field types I need immediately. Hopefully, it might help others having the same problem.
public function reformatData($array) {
if (!is_array($array)) {
return false;
}
$cols = $this->info(Zend_Db_Table_Abstract::METADATA);
if (is_array($cols)) {
foreach ($cols as $col) {
if (array_key_exists($col['COLUMN_NAME'], $array)) {
switch ($col['DATA_TYPE']) {
case 'int': case 'tinyint':
if ($array[$col['COLUMN_NAME']] == '') {
$newArray[$col['COLUMN_NAME']] = new Zend_Db_Expr('null');
}
else {
$newArray[$col['COLUMN_NAME']] = $array[$col['COLUMN_NAME']];
}
break;
case 'date':
if ($array[$col['COLUMN_NAME']] == '') {
$newArray[$col['COLUMN_NAME']] = new Zend_Db_Expr('null');
}
elseif(!Zend_Date::isDate($array[$col['COLUMN_NAME']], 'YYYY-MM-dd')) {
$date = new Zend_Date($array[$col['COLUMN_NAME']], null, 'en_GB');
$newArray[$col['COLUMN_NAME']] = $date->toString('YYYY-MM-dd');
}
else {
$newArray[$col['COLUMN_NAME']] = $array[$col['COLUMN_NAME']];
}
break;
case 'datetime':
if ($array[$col['COLUMN_NAME']] == '') {
$newArray[$col['COLUMN_NAME']] = new Zend_Db_Expr('null');
}
elseif(!Zend_Date::isDate($array[$col['COLUMN_NAME']], 'YYYY-MM-dd HH:MM')) {
$date = new Zend_Date($array[$col['COLUMN_NAME']], null, 'en_GB');
$newArray[$col['COLUMN_NAME']] = $date->toString('YYYY-MM-dd HH:MM');
}
else {
$newArray[$col['COLUMN_NAME']] = $array[$col['COLUMN_NAME']];
}
break;
default:
$newArray[$col['COLUMN_NAME']] = $array[$col['COLUMN_NAME']];
break;
}
}
}
return $newArray;
}
else {
return false;
}
}