有没有办法设置等效的"插入忽略"在cakephp?我不想使用Model-> query();.还有其他办法吗?
答案 0 :(得分:0)
这是我在1.3:
上用来抑制重复的内容文件:app / app_model.php
/**
*
* Callback executed when a save has failed.
* Contains database error parsing and evaluation to display appropriate messages to end-users.
*
*/
private function afterSaveFailed() {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$lastError = $db->lastError();
// this holds the match for the key id
// add more for more database types
$dupe_check=array(
'mysql' => '/^\d+: Duplicate entry \'.*\' for key (\d+)$/i',
'postgres' => '/^ERROR: duplicate key value violates .+ "(.+)"$/i',
);
// this holds the match for the key id
// add more for more database types
$foreign_check=array(
'postgres' => '/^ERROR: insert or update on table "(.+)" violates foreign key constraint .+/i',
);
if(preg_match($dupe_check[$db->config['driver']], $lastError, $matches)
&& !empty($dupe_check[$db->config['driver']])) {
$matches[1] = str_replace('_key','',$matches[1]);
$matches[1] = str_replace($this->table.'_','',$matches[1]);
$this->invalidate('db','Error: Duplicate value found.');
return;
}
if(preg_match($foreign_check[$db->config['driver']], $lastError, $matches)
&& !empty($foreign_check[$db->config['driver']])) {
$this->invalidate('db','Error: Referenced value not found.');
return;
}
}