Skip to content

Commit

Permalink
Fixes #4. Remove primary key check. The onus is on the user to have p…
Browse files Browse the repository at this point in the history
…rimary key.
  • Loading branch information
yadakhov committed Jun 23, 2016
1 parent d5e5ee2 commit bb90302
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 42 deletions.
32 changes: 0 additions & 32 deletions src/InsertOnDuplicateKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public static function insertOnDuplicateKey(array $data, array $updateColumns =
$data = [$data];
}

static::checkPrimaryKeyExists($data);

$sql = static::buildInsertOnDuplicateSql($data, $updateColumns);

$data = static::inLineArray($data);
Expand All @@ -58,8 +56,6 @@ public static function insertIgnore(array $data)
$data = [$data];
}

static::checkPrimaryKeyExists($data);

$sql = static::buildInsertIgnoreSql($data);

$data = static::inLineArray($data);
Expand All @@ -85,8 +81,6 @@ public static function replace(array $data)
$data = [$data];
}

static::checkPrimaryKeyExists($data);

$sql = static::buildReplaceSql($data);

$data = static::inLineArray($data);
Expand Down Expand Up @@ -163,32 +157,6 @@ protected static function getFirstRow(array $data)
return $first;
}

/**
* Check to make sure the first row as the primary key.
* Every row needs to have the primary key but we will only check the first row for efficiency.
*
* @param array $data
*/
protected static function checkPrimaryKeyExists(array $data)
{
// Check to make sure $data contains the primary key
$primaryKey = static::getPrimaryKey();
$hasKey = false;

$first = static::getFirstRow($data);

foreach (array_keys($first) as $key) {
if ($key === $primaryKey) {
$hasKey = true;
break;
}
}

if ($hasKey === false) {
throw new \InvalidArgumentException(sprintf('Missing primary key %s.', $primaryKey));
}
}

/**
* Build a value list.
*
Expand Down
10 changes: 0 additions & 10 deletions tests/MainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,6 @@ public function testBuildInsertOnDuplicateSqlMultipleWithUpdateColumn()
$this->assertEquals($expected, $result);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testInsertWithBadId()
{
$data = ['incorrect_id_field' => 1, 'email' => 'user1@email.com', 'name' => 'User One'];

$this->user->insertOnDuplicateKey($data);
}

public function testBuildInsertIgnoreSqlSimple()
{
$data = [
Expand Down

0 comments on commit bb90302

Please sign in to comment.