Created
September 23, 2015 00:06
-
-
Save ryzr/8435462c5d48805b7314 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Themes\Pharmacy4u\Models\Traits; | |
trait InsertIgnoreModelTrait | |
{ | |
public static function insertIgnore($values){ | |
$self = new static(); | |
$query = $self->newBaseQueryBuilder(); | |
if (empty($values)) return true; | |
// Since every insert gets treated like a batch insert, we will make sure the | |
// bindings are structured in a way that is convenient for building these | |
// inserts statements by verifying the elements are actually an array. | |
if ( ! is_array(reset($values))) | |
{ | |
$values = array($values); | |
} | |
// Since every insert gets treated like a batch insert, we will make sure the | |
// bindings are structured in a way that is convenient for building these | |
// inserts statements by verifying the elements are actually an array. | |
else | |
{ | |
foreach ($values as $key => $value) | |
{ | |
ksort($value); $values[$key] = $value; | |
} | |
} | |
// We'll treat every insert like a batch insert so we can easily insert each | |
// of the records into the database consistently. This will make it much | |
// easier on the grammars to just handle one type of record insertion. | |
$bindings = array(); | |
foreach ($values as $record) | |
{ | |
foreach ($record as $value) | |
{ | |
$bindings[] = $value; | |
} | |
} | |
$sql = $self->compileInsertIgnore($query, $values); | |
//QueryBuilder cleanBindings function is a protected function... but here's the code anyway | |
$bindings = array_values(array_filter($bindings, function($binding) | |
{ | |
return ! $binding instanceof Expression; | |
})); | |
return $query->getConnection()->insert($sql, $bindings); | |
} | |
/** | |
* Compile an insert statement into SQL. | |
* | |
* @param \Illuminate\Database\Query\Builder $query | |
* @param array $values | |
* @return string | |
*/ | |
public function compileInsertIgnore(\Illuminate\Database\Query\Builder $query, array $values) | |
{ | |
// Essentially we will force every insert to be treated as a batch insert which | |
// simply makes creating the SQL easier for us since we can utilize the same | |
// basic routine regardless of an amount of records given to us to insert. | |
$table = $query->getGrammar()->wrapTable($this->table); | |
if ( ! is_array(reset($values))) | |
{ | |
$values = array($values); | |
} | |
$columns = $query->getGrammar()->columnize(array_keys(reset($values))); | |
// We need to build a list of parameter place-holders of values that are bound | |
// to the query. Each insert should have the exact same amount of parameter | |
// bindings so we will loop through the record and parameterize them all. | |
$parameters = array(); | |
foreach($values as $record) | |
{ | |
$parameters[] = '('.$query->getGrammar()->parameterize($record).')'; | |
} | |
$parameters = implode(', ', $parameters); | |
return "insert ignore into $table ($columns) values $parameters"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment