Skip to content

Commit

Permalink
Selection: fixed insert spoiling PDO:lastInsertId (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stopka authored and dg committed Feb 8, 2019
1 parent 6af5cbf commit 2eb679f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Database/Table/Selection.php
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,10 @@ protected function refreshData(): void
*/
public function insert(iterable $data)
{
//should be called before query for not to spoil PDO::lastInsertId
$primarySequenceName = $this->getPrimarySequence();
$primaryAutoincrementKey = $this->context->getStructure()->getPrimaryAutoincrementKey($this->name);

if ($data instanceof self) {
$return = $this->context->queryArgs($this->sqlBuilder->buildInsertQuery() . ' ' . $data->getSql(), $data->getSqlBuilder()->getParameters());

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

$primarySequenceName = $this->getPrimarySequence();
$primaryAutoincrementKey = $this->context->getStructure()->getPrimaryAutoincrementKey($this->name);

$primaryKey = [];
foreach ((array) $this->primary as $key) {
if (isset($data[$key])) {
Expand Down
46 changes: 46 additions & 0 deletions tests/Database/Table/bugs/bug216.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* Test: bug #216
* @dataProvider? ../databases.ini
*/

declare(strict_types=1);

use Tester\Assert;

require __DIR__ . '/../../../bootstrap.php';

//Prepare connection
$options = Tester\Environment::loadData() + ['user' => null, 'password' => null];

try {
$connection = new Nette\Database\Connection($options['dsn'], $options['user'], $options['password']);
} catch (PDOException $e) {
Tester\Environment::skip("Connection to '$options[dsn]' failed. Reason: " . $e->getMessage());
}

if (strpos($options['dsn'], 'sqlite::memory:') === false) {
Tester\Environment::lock($options['dsn'], TEMP_DIR);
}

$driverName = $connection->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME);
$cacheMemoryStorage = new Nette\Caching\Storages\MemoryStorage;

$structure = new Nette\Database\Structure($connection, $cacheMemoryStorage);
$conventions = new Nette\Database\Conventions\StaticConventions();
$context = new Nette\Database\Context($connection, $structure, $conventions, $cacheMemoryStorage);

//Testing
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../files/{$driverName}-nette_test1.sql");

$book = $context->table('author')->insert([
'name' => $context->literal('LOWER(?)', 'Eddard Stark'),
'web' => 'http://example.com',
'born' => new \DateTime('2011-11-11'),
]); // INSERT INTO `author` (`name`, `web`) VALUES (LOWER('Eddard Stark'), 'http://example.com', '2011-11-11 00:00:00')
// id = 14

Assert::type(Nette\Database\Table\ActiveRow::class, $book);
Assert::equal('eddard stark', $book->name);
Assert::equal(new Nette\Utils\DateTime('2011-11-11'), $book->born);

0 comments on commit 2eb679f

Please sign in to comment.