Skip to content

Commit 857b365

Browse files
committed
String primary keys should not be type checked
1 parent 6904dbf commit 857b365

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

Tests/Unit/MiscellaneousTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44

55
class MiscellaneousTest extends \PHPUnit\Framework\TestCase
66
{
7-
public function testNoStringPrimaryKey() : void
8-
{
9-
$customer = new \Tests\App\Record\Customer(1);
10-
$this->assertTrue($customer->loaded());
11-
$this->expectException(\PHPFUI\ORM\Exception::class);
12-
$customer = new \Tests\App\Record\Customer('test');
13-
}
14-
157
public function testRow() : void
168
{
179
$row = \PHPFUI\ORM::getRow('select * from customer');

src/PHPFUI/ORM/Record.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,28 @@ public function __construct(int|array|null|string $parameter = null)
6363

6464
switch ($type)
6565
{
66-
case 'integer':
67-
$type = 'int';
68-
// Intentionally fall through
6966
case 'string':
7067

71-
if (1 == \count(static::$primaryKeys) && $type == static::$fields[static::$primaryKeys[0]][self::PHP_TYPE_INDEX])
68+
if (1 == \count(static::$primaryKeys))
69+
{
70+
$this->read($parameter);
71+
}
72+
else
73+
{
74+
throw new \PHPFUI\ORM\Exception(static::class . ' has no string primary key');
75+
}
76+
77+
break;
78+
79+
case 'integer':
80+
81+
if (1 == \count(static::$primaryKeys) && 'int' == static::$fields[static::$primaryKeys[0]][self::PHP_TYPE_INDEX])
7282
{
7383
$this->read($parameter);
7484
}
7585
else
7686
{
77-
throw new \PHPFUI\ORM\Exception(static::class . ' has no ' . $type . ' primary key');
87+
throw new \PHPFUI\ORM\Exception(static::class . ' does not have an integer primary key');
7888
}
7989

8090
break;

0 commit comments

Comments
 (0)