2011-11-21 23 views
1

cakephpに "insert ignore"を設定する方法はありますか?私はModel-> query();を使いたくありません。他の方法はありますか?cakephpの "duplicate-entry"エラーを抑制または管理します

+0

http://stackoverflow.com/questions/2830808/cakephp-insert-ignore-フィーチャー –

+0

ありがとうございます。それを見つけることができませんでした。 – Chobeat

答えて

0

これは私が1.3に重複を抑制するために使用するものである:

ファイル:アプリ/ 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; 
    } 
} 
関連する問題