Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
【CodeIgniter3】クエリビルダーにINSERT IGNORE構文を追加する方法
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class CI_DB extends MY_DB_query_builder
{
}
class MY_DB_query_builder extends CI_DB_query_builder
{
/**
* Insert Ignore
*
* Compiles an insert string and runs the query
*
* @param string the table to insert data into
* @param array an associative array of insert values
* @param bool $escape Whether to escape values and identifiers
* @return object
*/
public function insert_ignore($table = '', $set = NULL, $escape = NULL)
{
if ($set !== NULL)
{
$this->set($set, '', $escape);
}
if ($this->_validate_insert($table) === FALSE)
{
return FALSE;
}
$sql = $this->_insert_ignore(
$this->protect_identifiers(
$this->qb_from[0], TRUE, $escape, FALSE
),
array_keys($this->qb_set),
array_values($this->qb_set)
);
$this->_reset_write();
return $this->query($sql);
}
/**
* Insert Ignore statement
*
* Generates a platform-specific insert string from the supplied data
*
* @param string the table name
* @param array the insert keys
* @param array the insert values
* @return string
*/
protected function _insert_ignore($table, $keys, $values)
{
return 'INSERT IGNORE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment