Skip to content

Commit 7f1f53a

Browse files
authored
修复有多个主键的表在生成模型时,主键顺序可能不正确 (#584)
1 parent c2a8802 commit 7f1f53a

File tree

11 files changed

+25
-29
lines changed

11 files changed

+25
-29
lines changed

src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ private function getClassName(string $table, array $prefixs): string
292292
*/
293293
private function parseFields(?string $poolName, array $fields, ?array &$data, bool $isView, string $table, ?array $config): void
294294
{
295-
$idCount = 0;
296295
foreach ($fields as $field)
297296
{
298297
$atttypmod = $field['atttypmod'];
@@ -331,7 +330,7 @@ private function parseFields(?string $poolName, array $fields, ?array &$data, bo
331330
'default' => $field['adsrc'],
332331
'defaultValue' => $this->parseFieldDefaultValue($poolName, $type, $field['adsrc']),
333332
'isPrimaryKey' => $isPk,
334-
'primaryKeyIndex' => $field['ordinal_position'] ?? -1,
333+
'primaryKeyIndex' => $primaryKeyIndex = ($field['ordinal_position'] ?? 0) - 1,
335334
'isAutoIncrement' => '' !== $field['attidentity'],
336335
'comment' => $field['description'] ?? '',
337336
'typeDefinition' => $config['relation'][$table]['fields'][$field['attname']]['typeDefinition'] ?? true,
@@ -340,10 +339,11 @@ private function parseFields(?string $poolName, array $fields, ?array &$data, bo
340339
];
341340
if ($isPk)
342341
{
343-
$data['table']['id'][] = $field['attname'];
344-
++$idCount;
342+
$data['table']['id'][$primaryKeyIndex] = $field['attname'];
345343
}
346344
}
345+
ksort($data['table']['id']);
346+
$data['table']['id'] = array_values($data['table']['id']);
347347
}
348348

349349
/**

src/Components/pgsql/tests/Model/Base/ArticleBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ abstract class ArticleBase extends Model
3737
/**
3838
* id.
3939
*
40-
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=true, ndims=0, virtual=false)
40+
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false)
4141
*/
4242
protected ?int $id = null;
4343

src/Components/pgsql/tests/Model/Base/MemberBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class MemberBase extends Model
3636
/**
3737
* id.
3838
*
39-
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=true, ndims=0, virtual=false)
39+
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false)
4040
*/
4141
protected ?int $id = null;
4242

src/Components/pgsql/tests/Model/Base/NoIncPkBase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class NoIncPkBase extends Model
3636
/**
3737
* a_id.
3838
*
39-
* @Column(name="a_id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=false, ndims=0, virtual=false)
39+
* @Column(name="a_id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=false, ndims=0, virtual=false)
4040
*/
4141
protected ?int $aId = null;
4242

@@ -65,7 +65,7 @@ public function setAId(?int $aId)
6565
/**
6666
* b_id.
6767
*
68-
* @Column(name="b_id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=2, isAutoIncrement=false, ndims=0, virtual=false)
68+
* @Column(name="b_id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=false, ndims=0, virtual=false)
6969
*/
7070
protected ?int $bId = null;
7171

src/Components/pgsql/tests/Model/Base/PerformanceBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ abstract class PerformanceBase extends Model
3535
/**
3636
* id.
3737
*
38-
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=true, ndims=0, virtual=false)
38+
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false)
3939
*/
4040
protected ?int $id = null;
4141

src/Components/pgsql/tests/Model/Base/TestJsonBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ abstract class TestJsonBase extends Model
3535
/**
3636
* id.
3737
*
38-
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=true, ndims=0, virtual=false)
38+
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false)
3939
*/
4040
protected ?int $id = null;
4141

src/Components/pgsql/tests/Model/Base/TestSoftDeleteBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class TestSoftDeleteBase extends Model
3636
/**
3737
* id.
3838
*
39-
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=true, ndims=0, virtual=false)
39+
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false)
4040
*/
4141
protected ?int $id = null;
4242

src/Components/pgsql/tests/Model/Base/TreeBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class TreeBase extends Model
3636
/**
3737
* id.
3838
*
39-
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=true, ndims=0, virtual=false)
39+
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false)
4040
*/
4141
protected ?int $id = null;
4242

src/Components/pgsql/tests/Model/Base/UpdateTimeBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ abstract class UpdateTimeBase extends Model
4545
/**
4646
* id.
4747
*
48-
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=true, ndims=0, virtual=false)
48+
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false)
4949
*/
5050
protected ?int $id = null;
5151

src/Components/pgsql/tests/Model/Base/VirtualColumnBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class VirtualColumnBase extends Model
3636
/**
3737
* id.
3838
*
39-
* @Column(name="id", type="int8", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=true, ndims=0, virtual=false)
39+
* @Column(name="id", type="int8", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false)
4040
*/
4141
protected ?int $id = null;
4242

0 commit comments

Comments
 (0)