Skip to content

Commit 6904dbf

Browse files
committed
primaryKeys array is no longer associative
1 parent ead0abb commit 6904dbf

File tree

13 files changed

+40
-43
lines changed

13 files changed

+40
-43
lines changed

Tests/Fixtures/Record/Definition/Order.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ abstract class Order extends \PHPFUI\ORM\Record
6161
'taxes' => ['decimal(19,4)', 'float', 19, false, true, 0, ],
6262
];
6363

64-
/** @var array<string, true> */
65-
protected static array $primaryKeys = ['order_id' => true, ];
64+
/** @var array<string> */
65+
protected static array $primaryKeys = ['order_id', ];
6666

6767
protected static string $table = 'order';
6868
}

Tests/Fixtures/Record/Definition/OrderDetail.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ abstract class OrderDetail extends \PHPFUI\ORM\Record
4141
'unit_price' => ['decimal(19,4)', 'float', 19, false, true, 0, ],
4242
];
4343

44-
/** @var array<string, true> */
45-
protected static array $primaryKeys = ['order_detail_id' => true, ];
44+
/** @var array<string> */
45+
protected static array $primaryKeys = ['order_detail_id', ];
4646

4747
protected static string $table = 'order_detail';
4848
}

Tests/Fixtures/Record/Definition/Product.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ abstract class Product extends \PHPFUI\ORM\Record
4242
'target_level' => ['integer', 'int', 0, false, true, null, ],
4343
];
4444

45-
/** @var array<string, true> */
46-
protected static array $primaryKeys = ['product_id' => true, ];
45+
/** @var array<string> */
46+
protected static array $primaryKeys = ['product_id', ];
4747

4848
protected static string $table = 'product';
4949
}

Tests/Unit/MiscellaneousTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
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+
715
public function testRow() : void
816
{
917
$row = \PHPFUI\ORM::getRow('select * from customer');

src/PHPFUI/ORM/Children.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected function getTable(string $class) : \PHPFUI\ORM\Table
5050
$childTable = new $class();
5151
$condition = new \PHPFUI\ORM\Condition();
5252

53-
foreach ($this->currentRecord->getPrimaryKeys() as $primaryKey => $junk)
53+
foreach ($this->currentRecord->getPrimaryKeys() as $primaryKey)
5454
{
5555
$condition->and($primaryKey, $this->currentRecord->{$primaryKey});
5656
}

src/PHPFUI/ORM/ManyToMany.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ public function getValue(array $parameters) : mixed
3333
$relatedTable->addJoin($junctionTableName, $relatedTableName . \PHPFUI\ORM::$idSuffix);
3434
$condition = new \PHPFUI\ORM\Condition();
3535

36-
foreach ($this->currentRecord->getPrimaryKeys() as $primaryKey => $junk)
36+
foreach ($this->currentRecord->getPrimaryKeys() as $primaryKey)
3737
{
3838
$condition->and($junctionTableName . '.' . $primaryKey, $this->currentRecord->{$primaryKey});
3939
}
4040

41-
foreach ($relatedTable->getPrimaryKeys() as $primaryKey => $junk)
41+
foreach ($relatedTable->getPrimaryKeys() as $primaryKey)
4242
{
4343
$condition->and(new \PHPFUI\ORM\Field($junctionTableName . '.' . $primaryKey), new \PHPFUI\ORM\Field($relatedTableName . '.' . $primaryKey));
4444
}

src/PHPFUI/ORM/Record.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,33 +63,22 @@ public function __construct(int|array|null|string $parameter = null)
6363

6464
switch ($type)
6565
{
66-
case 'string':
67-
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-
7966
case 'integer':
67+
$type = 'int';
68+
// Intentionally fall through
69+
case 'string':
8070

81-
if (1 == \count(static::$primaryKeys) && 'int' == static::$fields[\array_key_first(static::$primaryKeys)][self::PHP_TYPE_INDEX])
71+
if (1 == \count(static::$primaryKeys) && $type == static::$fields[static::$primaryKeys[0]][self::PHP_TYPE_INDEX])
8272
{
8373
$this->read($parameter);
8474
}
8575
else
8676
{
87-
throw new \PHPFUI\ORM\Exception(static::class . ' does not have an integer primary key');
77+
throw new \PHPFUI\ORM\Exception(static::class . ' has no ' . $type . ' primary key');
8878
}
8979

9080
break;
9181

92-
9382
case 'array':
9483

9584
$this->read($parameter);
@@ -360,7 +349,7 @@ public function getLength(string $field) : int
360349
}
361350

362351
/**
363-
* @return array primary keys
352+
* @return array<string> primary keys
364353
*/
365354
public static function getPrimaryKeys() : array
366355
{
@@ -374,7 +363,7 @@ public function getPrimaryKeyValues() : array
374363
{
375364
$retVal = [];
376365

377-
foreach (static::$primaryKeys as $key => $junk)
366+
foreach (static::$primaryKeys as $key)
378367
{
379368
$retVal[$key] = $this->current[$key] ?? null;
380369
}
@@ -518,7 +507,7 @@ public function reload() : bool
518507
{
519508
$keys = [];
520509

521-
foreach (static::$primaryKeys as $key => $junk)
510+
foreach (static::$primaryKeys as $key)
522511
{
523512
if (\array_key_exists($key, $this->current))
524513
{
@@ -608,7 +597,7 @@ public function update() : bool
608597
{
609598
if (isset(static::$fields[$field]))
610599
{
611-
if (! isset(static::$primaryKeys[$field]))
600+
if (! \in_array($field, static::$primaryKeys))
612601
{
613602
if (empty($value) && \in_array(static::$fields[$field][self::MYSQL_TYPE_INDEX], $dateTimes))
614603
{
@@ -786,15 +775,15 @@ private function buildWhere(array|int|string $key, array &$input) : string
786775

787776
if (! \is_array($key))
788777
{
789-
$key = [\array_key_first(static::$primaryKeys) => $key];
778+
$key = [static::$primaryKeys[0] => $key];
790779
}
791780
else
792781
{ // if all primary keys are set, then use primary keys only
793782

794783
$keys = [];
795784
$all = true;
796785

797-
foreach (static::$primaryKeys as $keyField => $junk)
786+
foreach (static::$primaryKeys as $keyField)
798787
{
799788
if (! isset($key[$keyField]))
800789
{
@@ -854,7 +843,7 @@ private function privateInsert(bool $updateOnDuplicate, string $ignore = '') : i
854843
continue;
855844
}
856845

857-
if (! static::$autoIncrement || ! (isset(static::$primaryKeys[$key]) && empty($value)))
846+
if (! static::$autoIncrement || ! (\in_array($key, static::$primaryKeys) && empty($value)))
858847
{
859848
$sql .= $comma . '`' . $key . '`';
860849
$input[] = $value;
@@ -883,7 +872,7 @@ private function privateInsert(bool $updateOnDuplicate, string $ignore = '') : i
883872
continue;
884873
}
885874

886-
if (! isset(static::$primaryKeys[$key]))
875+
if (! \in_array($key, static::$primaryKeys))
887876
{
888877
$updateSql .= $comma . '`' . $key . '` = ?';
889878
$input[] = $value;
@@ -906,7 +895,7 @@ private function privateInsert(bool $updateOnDuplicate, string $ignore = '') : i
906895

907896
if (static::$autoIncrement && $returnValue)
908897
{
909-
$this->current[\array_key_first(static::$primaryKeys)] = $returnValue = (int)\PHPFUI\ORM::lastInsertId(\array_key_first(static::$primaryKeys));
898+
$this->current[static::$primaryKeys[0]] = $returnValue = (int)\PHPFUI\ORM::lastInsertId(static::$primaryKeys[0]);
910899
}
911900

912901
$this->loaded = true; // record is effectively read from the database now

src/PHPFUI/ORM/Record/Definition/Migration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ abstract class Migration extends \PHPFUI\ORM\Record
2020
'ran' => ['timestamp', 'string', 20, false, null, ],
2121
];
2222

23-
/** @var array<string, true> */
24-
protected static array $primaryKeys = ['migrationId' => true, ];
23+
/** @var array<string> */
24+
protected static array $primaryKeys = ['migrationId', ];
2525

2626
protected static string $table = 'migration';
2727
}

src/PHPFUI/ORM/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ public function updateFromTable(array $request) : bool
958958

959959
if (\count($primaryKeys))
960960
{
961-
$mainKey = \array_key_first($primaryKeys);
961+
$mainKey = $primaryKeys[0];
962962

963963
foreach ($request[$mainKey] ?? [] as $existingKey => $index)
964964
{

src/PHPFUI/ORM/Tool/Generate/Base.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public function nameSort(\PHPFUI\ORM\Schema\Field $lhs, \PHPFUI\ORM\Schema\Field
1313

1414
abstract protected function getLine(\PHPFUI\ORM\Schema\Field $field) : string;
1515

16+
/**
17+
* @return array<string, string>
18+
*/
1619
protected function getPrimaryKeys(string $table) : array
1720
{
1821
$primaryKeys = [];

src/PHPFUI/ORM/Tool/Generate/CRUD.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ abstract class ~~CLASS~~ extends \PHPFUI\ORM\Record
3434
// MYSQL_TYPE, PHP_TYPE, LENGTH, ALLOWS_NULL, DEFAULT
3535
~~FIELD_ARRAY~~ ];
3636
37-
/** @var array<string, true> */
37+
/** @var array<string> */
3838
protected static array $primaryKeys = ~~PRIMARY_KEY~~;
3939
4040
protected static string $table = '~~TABLE_NAME~~';
@@ -98,7 +98,7 @@ class ~~CLASS~~ extends \PHPFUI\ORM\Table
9898

9999
foreach ($this->getPrimaryKeys($table) as $key)
100100
{
101-
$keys .= "'{$key}' => true, ";
101+
$keys .= "'{$key}', ";
102102
}
103103
$keys .= ']';
104104
$replaceVars = [$ucTable, $table, $fieldComments, $fieldArray, $keys, $autoIncrement, \PHPFUI\ORM::$recordNamespace, \PHPFUI\ORM::$tableNamespace, ];

src/PHPFUI/ORM/Tool/Generate/Validator.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ public function generate(string $table) : bool
1818

1919
$ucTable = \PHPFUI\ORM::getBaseClassName($table);
2020

21-
$keys = $this->getPrimaryKeys($table);
22-
2321
$classDefinition = <<<'PHP'
2422
<?php
2523

src/PHPFUI/ORM/Validator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
* | gt_field | Greater Than field | field, required |
3939
* | gte_field | Greater Than or Equal to field | field, required |
4040
* | icontains | Field must contain (case insensitive) | comma separated list of strings |
41-
* | iends_with | Field must end with (case insensitive) | comma separated list of strings |
4241
* | integer | Whole number, no fractional part | None |
4342
* | istarts_with | Field must start with (case insensitive) | comma separated list of strings |
4443
* | lt_field | Less Than field | field, required |
@@ -800,7 +799,7 @@ private function validate_unique(mixed $value, array $parameters, array $fieldDe
800799

801800
if (1 == \count($primaryKeys))
802801
{
803-
$primaryKey = \array_key_first($primaryKeys);
802+
$primaryKey = $primaryKeys[0];
804803
$condition->and($primaryKey, $this->record->{$primaryKey}, new \PHPFUI\ORM\Operator\NotEqual());
805804
}
806805
$field = $this->currentField;

0 commit comments

Comments
 (0)