Skip to content

Commit 67126dc

Browse files
committed
Merge pull request #82 from Unlink/zero-pk-fix
Selection: fixed bug with zero in primary key
2 parents e72059c + c377bf6 commit 67126dc

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/Database/Table/Selection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,8 @@ protected function execute()
506506
foreach ($result->getPdoStatement() as $key => $row) {
507507
$row = $this->createRow($result->normalizeRow($row));
508508
$primary = $row->getSignature(FALSE);
509-
$usedPrimary = $usedPrimary && $primary;
510-
$this->rows[$primary ?: $key] = $row;
509+
$usedPrimary = $usedPrimary && (string) $primary !== '';
510+
$this->rows[$usedPrimary ? $primary : $key] = $row;
511511
}
512512
$this->data = $this->rows;
513513

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* Test: Zero Primary key bug
5+
* @dataProvider? ../../databases.ini mysql
6+
*/
7+
8+
use Tester\Assert;
9+
10+
require __DIR__ . '/../../connect.inc.php';
11+
12+
$context->query('CREATE DATABASE IF NOT EXISTS nette_test');
13+
$context->query('USE nette_test');
14+
15+
$context->query('
16+
CREATE TABLE ships (
17+
id INTEGER PRIMARY KEY NOT NULL,
18+
name TEXT NOT NULL
19+
);
20+
');
21+
22+
$context->query('
23+
INSERT INTO ships (id, name) VALUES(2, "Enterprise");
24+
');
25+
26+
$context->query('
27+
INSERT INTO ships (id, name) VALUES(0, "Endeavour");
28+
');
29+
30+
Assert::same(2, $context->table('ships')->order('id DESC')->count());
31+
32+
$result = $context->table('ships')->order('id DESC')->fetchAll(); // SELECT * FROM `ships` ORDER BY id DESC
33+
34+
Assert::same("Enterprise", $result[2]->name);
35+
36+
Assert::same("Endeavour", $result[0]->name);

0 commit comments

Comments
 (0)