Skip to content

Commit 2eb679f

Browse files
Stopkadg
authored andcommitted
Selection: fixed insert spoiling PDO:lastInsertId (#217)
1 parent 6af5cbf commit 2eb679f

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/Database/Table/Selection.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,10 @@ protected function refreshData(): void
739739
*/
740740
public function insert(iterable $data)
741741
{
742+
//should be called before query for not to spoil PDO::lastInsertId
743+
$primarySequenceName = $this->getPrimarySequence();
744+
$primaryAutoincrementKey = $this->context->getStructure()->getPrimaryAutoincrementKey($this->name);
745+
742746
if ($data instanceof self) {
743747
$return = $this->context->queryArgs($this->sqlBuilder->buildInsertQuery() . ' ' . $data->getSql(), $data->getSqlBuilder()->getParameters());
744748

@@ -756,9 +760,6 @@ public function insert(iterable $data)
756760
return $return->getRowCount();
757761
}
758762

759-
$primarySequenceName = $this->getPrimarySequence();
760-
$primaryAutoincrementKey = $this->context->getStructure()->getPrimaryAutoincrementKey($this->name);
761-
762763
$primaryKey = [];
763764
foreach ((array) $this->primary as $key) {
764765
if (isset($data[$key])) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/**
4+
* Test: bug #216
5+
* @dataProvider? ../databases.ini
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
use Tester\Assert;
11+
12+
require __DIR__ . '/../../../bootstrap.php';
13+
14+
//Prepare connection
15+
$options = Tester\Environment::loadData() + ['user' => null, 'password' => null];
16+
17+
try {
18+
$connection = new Nette\Database\Connection($options['dsn'], $options['user'], $options['password']);
19+
} catch (PDOException $e) {
20+
Tester\Environment::skip("Connection to '$options[dsn]' failed. Reason: " . $e->getMessage());
21+
}
22+
23+
if (strpos($options['dsn'], 'sqlite::memory:') === false) {
24+
Tester\Environment::lock($options['dsn'], TEMP_DIR);
25+
}
26+
27+
$driverName = $connection->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME);
28+
$cacheMemoryStorage = new Nette\Caching\Storages\MemoryStorage;
29+
30+
$structure = new Nette\Database\Structure($connection, $cacheMemoryStorage);
31+
$conventions = new Nette\Database\Conventions\StaticConventions();
32+
$context = new Nette\Database\Context($connection, $structure, $conventions, $cacheMemoryStorage);
33+
34+
//Testing
35+
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../files/{$driverName}-nette_test1.sql");
36+
37+
$book = $context->table('author')->insert([
38+
'name' => $context->literal('LOWER(?)', 'Eddard Stark'),
39+
'web' => 'http://example.com',
40+
'born' => new \DateTime('2011-11-11'),
41+
]); // INSERT INTO `author` (`name`, `web`) VALUES (LOWER('Eddard Stark'), 'http://example.com', '2011-11-11 00:00:00')
42+
// id = 14
43+
44+
Assert::type(Nette\Database\Table\ActiveRow::class, $book);
45+
Assert::equal('eddard stark', $book->name);
46+
Assert::equal(new Nette\Utils\DateTime('2011-11-11'), $book->born);

0 commit comments

Comments
 (0)