diff --git a/db.class.php b/db.class.php index 080a603..938a434 100644 --- a/db.class.php +++ b/db.class.php @@ -179,7 +179,7 @@ class DB call_user_func_array('DB::queryNull', $args); } - public static function insertOrReplace($which, $table, $datas) { + public static function insertOrReplace($which, $table, $datas, $options=array()) { $datas = unserialize(serialize($datas)); // break references within array $keys = null; @@ -213,16 +213,16 @@ class DB $keys_str = implode(', ', DB::wrapStr($keys, '`')); $values_str = implode(',', $values); - DB::queryNull("$which INTO $table ($keys_str) VALUES $values_str"); + if (isset($options['ignore']) && $options['ignore'] && strtolower($which) == 'insert') { + DB::queryNull("INSERT IGNORE INTO $table ($keys_str) VALUES $values_str"); + } else { + DB::queryNull("$which INTO $table ($keys_str) VALUES $values_str"); + } } - public static function insert($table, $data) { - return DB::insertOrReplace('INSERT', $table, $data); - } - - public static function replace($table, $data) { - return DB::insertOrReplace('REPLACE', $table, $data); - } + public static function insert($table, $data) { return DB::insertOrReplace('INSERT', $table, $data); } + public static function insertIgnore($table, $data) { return DB::insertOrReplace('INSERT', $table, $data, array('ignore' => true)); } + public static function replace($table, $data) { return DB::insertOrReplace('REPLACE', $table, $data); } public static function delete() { $args = func_get_args(); diff --git a/simpletest/BasicTest.php b/simpletest/BasicTest.php index da92559..e5e82b8 100644 --- a/simpletest/BasicTest.php +++ b/simpletest/BasicTest.php @@ -222,6 +222,16 @@ class BasicTest extends SimpleTest { $this->assert($smile === $getsmile2); $this->assert($smile === $getsmile3); } + + function test_6_insert_ignore() { + DB::insertIgnore('accounts', array( + 'id' => 1, //duplicate primary key + 'username' => 'gonesoon', + 'password' => 'something', + 'age' => 61, + 'height' => 199.194 + )); + } }