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
4 changes: 2 additions & 2 deletions src/Database/Table/Selection.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,8 @@ protected function execute()
foreach ($result->getPdoStatement() as $key => $row) {
$row = $this->createRow($result->normalizeRow($row));
$primary = $row->getSignature(FALSE);
$usedPrimary = $usedPrimary && $primary;
$this->rows[$primary ?: $key] = $row;
$usedPrimary = $usedPrimary && (string) $primary !== '';
$this->rows[$usedPrimary ? $primary : $key] = $row;
}
$this->data = $this->rows;

Expand Down
36 changes: 36 additions & 0 deletions tests/Database/Table/bugs/ZeroPrimaryKey.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* Test: Zero Primary key bug
* @dataProvider? ../../databases.ini mysql
*/

use Tester\Assert;

require __DIR__ . '/../../connect.inc.php';

$context->query('CREATE DATABASE IF NOT EXISTS nette_test');
$context->query('USE nette_test');

$context->query('
CREATE TABLE ships (
id INTEGER PRIMARY KEY NOT NULL,
name TEXT NOT NULL
);
');

$context->query('
INSERT INTO ships (id, name) VALUES(2, "Enterprise");
');

$context->query('
INSERT INTO ships (id, name) VALUES(0, "Endeavour");
');

Assert::same(2, $context->table('ships')->order('id DESC')->count());

$result = $context->table('ships')->order('id DESC')->fetchAll(); // SELECT * FROM `ships` ORDER BY id DESC

Assert::same("Enterprise", $result[2]->name);

Assert::same("Endeavour", $result[0]->name);