Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);