Skip to content

Commit

Permalink
Selection: fixed bug with zero in primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
Unlink authored and dg committed Sep 14, 2015
1 parent 9950571 commit 05d4c3b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Database/Table/Selection.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,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);

0 comments on commit 05d4c3b

Please sign in to comment.