-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code. Fix problem when $data is not an array or array. More …
…error checking.
- Loading branch information
Yada Khov
committed
May 13, 2016
1 parent
e0be9fb
commit 8d6af9d
Showing
3 changed files
with
123 additions
and
80 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
[Insert Duplicate Key Update](http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html) is a quick way to do mass insert. | ||
|
||
It's a trait meant to be used with Laravel's Eloquent ORM. | ||
|
||
### Code Example | ||
|
||
```php | ||
|
@@ -39,13 +41,16 @@ class UserTest extends Model | |
|
||
### created_at and updated_at fields. | ||
|
||
created_at and updated_at will not be updated automatically. You can pass the database in the $data array as: | ||
['id' => 1, 'email' => '[email protected]', 'name' => 'User One', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now(), ], | ||
created_at and updated_at will *not* be updated automatically. To update you can pass the fields in the insert array. | ||
|
||
```php | ||
['id' => 1, 'email' => '[email protected]', 'name' => 'User One', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()] | ||
``` | ||
|
||
### Will this work on Postgresql? | ||
|
||
No. On Duplicate Key Update is only available on MySQL. Postgresql 9.4 has a similar feature called [UPSERT](https://wiki.postgresql.org/wiki/UPSERT). | ||
|
||
### Isn't this the same as updateOrCreate()? | ||
|
||
It's similar but not the same. The updateOrCreate will only work on one row at a time. InsertOnDuplicateKey will on many rows. | ||
It is similar but not the same. The updateOrCreate() will only work for one row. InsertOnDuplicateKey will work on many rows. |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,23 +45,13 @@ public function testGetColumnListEmptyDataException() | |
$this->invokeMethod($this->user, 'getColumnList', [$data]); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
*/ | ||
public function testGetColumnListNotAssociativeArray() | ||
{ | ||
$data = [1, '[email protected]', 'User One'];; | ||
|
||
$result = $this->invokeMethod($this->user, 'getColumnList', [$data]); | ||
} | ||
|
||
public function testGetColumnList() | ||
{ | ||
$data = $this->getDataForInsert(); | ||
|
||
$expected = '`id`,`email`,`name`'; | ||
|
||
$result = $this->invokeMethod($this->user, 'getColumnList', [$data]); | ||
$result = $this->invokeMethod($this->user, 'getColumnList', [$data[0]]); | ||
|
||
$this->assertEquals($expected, $result); | ||
} | ||
|
@@ -72,7 +62,7 @@ public function testBuildValuesList() | |
|
||
$expected = '`id` = VALUES(`id`), `email` = VALUES(`email`), `name` = VALUES(`name`)'; | ||
|
||
$result = $this->invokeMethod($this->user, 'buildValuesList', [$data]); | ||
$result = $this->invokeMethod($this->user, 'buildValuesList', [$data[0]]); | ||
|
||
$this->assertEquals($expected, $result); | ||
} | ||
|
@@ -90,6 +80,17 @@ public function testInLineArraySimple() | |
$this->assertEquals($expected, $result); | ||
} | ||
|
||
public function testBuildQuestionMarks() | ||
{ | ||
$data = $this->getDataForInsert(); | ||
|
||
$expected = '(?,?,?), (?,?,?), (?,?,?)'; | ||
|
||
$result = $this->invokeMethod($this->user, 'buildQuestionMarks', [$data]); | ||
|
||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
public function testInLineArrayThreeRows() | ||
{ | ||
$data = $this->getDataForInsert(); | ||
|
@@ -132,4 +133,14 @@ public function testBuildSqlMultiple() | |
|
||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
*/ | ||
public function testInsertWithBadId() | ||
{ | ||
$data = ['incorrect_id_field' => 1, 'email' => '[email protected]', 'name' => 'User One']; | ||
|
||
$this->user->insertOnDuplicateKey($data); | ||
} | ||
} |