Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复有多个主键的表在生成模型时,主键顺序可能不正确 #584

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@
*/
private function parseFields(?string $poolName, array $fields, ?array &$data, bool $isView, string $table, ?array $config): void
{
$idCount = 0;
foreach ($fields as $field)
{
$atttypmod = $field['atttypmod'];
Expand Down Expand Up @@ -331,7 +330,7 @@
'default' => $field['adsrc'],
'defaultValue' => $this->parseFieldDefaultValue($poolName, $type, $field['adsrc']),
'isPrimaryKey' => $isPk,
'primaryKeyIndex' => $field['ordinal_position'] ?? -1,
'primaryKeyIndex' => $primaryKeyIndex = ($field['ordinal_position'] ?? 0) - 1,

Check warning on line 333 in src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php

View check run for this annotation

Codecov / codecov/patch

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

Added line #L333 was not covered by tests
'isAutoIncrement' => '' !== $field['attidentity'],
'comment' => $field['description'] ?? '',
'typeDefinition' => $config['relation'][$table]['fields'][$field['attname']]['typeDefinition'] ?? true,
Expand All @@ -340,10 +339,11 @@
];
if ($isPk)
{
$data['table']['id'][] = $field['attname'];
++$idCount;
$data['table']['id'][$primaryKeyIndex] = $field['attname'];

Check warning on line 342 in src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php

View check run for this annotation

Codecov / codecov/patch

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

Added line #L342 was not covered by tests
}
}
ksort($data['table']['id']);
$data['table']['id'] = array_values($data['table']['id']);

Check warning on line 346 in src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php

View check run for this annotation

Codecov / codecov/patch

src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php#L345-L346

Added lines #L345 - L346 were not covered by tests
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Components/pgsql/tests/Model/Base/ArticleBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract class ArticleBase extends Model
/**
* id.
*
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=true, ndims=0, virtual=false)
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false)
*/
protected ?int $id = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Components/pgsql/tests/Model/Base/MemberBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class MemberBase extends Model
/**
* id.
*
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=true, ndims=0, virtual=false)
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false)
*/
protected ?int $id = null;

Expand Down
4 changes: 2 additions & 2 deletions src/Components/pgsql/tests/Model/Base/NoIncPkBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class NoIncPkBase extends Model
/**
* a_id.
*
* @Column(name="a_id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=false, ndims=0, virtual=false)
* @Column(name="a_id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=false, ndims=0, virtual=false)
*/
protected ?int $aId = null;

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

Expand Down
2 changes: 1 addition & 1 deletion src/Components/pgsql/tests/Model/Base/PerformanceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class PerformanceBase extends Model
/**
* id.
*
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=true, ndims=0, virtual=false)
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false)
*/
protected ?int $id = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Components/pgsql/tests/Model/Base/TestJsonBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class TestJsonBase extends Model
/**
* id.
*
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=true, ndims=0, virtual=false)
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false)
*/
protected ?int $id = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class TestSoftDeleteBase extends Model
/**
* id.
*
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=true, ndims=0, virtual=false)
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false)
*/
protected ?int $id = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Components/pgsql/tests/Model/Base/TreeBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class TreeBase extends Model
/**
* id.
*
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=true, ndims=0, virtual=false)
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false)
*/
protected ?int $id = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Components/pgsql/tests/Model/Base/UpdateTimeBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ abstract class UpdateTimeBase extends Model
/**
* id.
*
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=true, ndims=0, virtual=false)
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false)
*/
protected ?int $id = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class VirtualColumnBase extends Model
/**
* id.
*
* @Column(name="id", type="int8", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=1, isAutoIncrement=true, ndims=0, virtual=false)
* @Column(name="id", type="int8", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false)
*/
protected ?int $id = null;

Expand Down
26 changes: 11 additions & 15 deletions src/Model/Cli/Model/ModelGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Imi\Model\Cli\Model\Event\Param\BeforeGenerateModel;
use Imi\Model\Cli\Model\Event\Param\BeforeGenerateModels;
use Imi\Model\Model;
use Imi\Util\ArrayUtil;
use Imi\Util\File;
use Imi\Util\Imi;
use Imi\Util\Text;
Expand Down Expand Up @@ -277,8 +278,10 @@
{
$typeDefinitions[$field['Field']] = ($tableConfig['fields'][$field['Field']]['typeDefinition'] ?? null) ?? ($configData['relation'][$table]['fields'][$field['Field']]['typeDefinition'] ?? true);
}
$pks = $query->execute(sprintf('SHOW KEYS FROM `%s`.`%s` where Key_name = \'PRIMARY\'', $database, $table))->getArray();
$pks = ArrayUtil::columnToKey($pks, 'Column_name');

Check warning on line 282 in src/Model/Cli/Model/ModelGenerate.php

View check run for this annotation

Codecov / codecov/patch

src/Model/Cli/Model/ModelGenerate.php#L281-L282

Added lines #L281 - L282 were not covered by tests

$this->parseFields($fields, $data, 'VIEW' === $item['TABLE_TYPE'], $typeDefinitions);
$this->parseFields($fields, $pks, $data, $typeDefinitions);

Check warning on line 284 in src/Model/Cli/Model/ModelGenerate.php

View check run for this annotation

Codecov / codecov/patch

src/Model/Cli/Model/ModelGenerate.php#L284

Added line #L284 was not covered by tests

$baseFileName = File::path($basePath, $className . 'Base.php');

Expand Down Expand Up @@ -337,20 +340,12 @@
/**
* 处理字段信息.
*/
private function parseFields(array $fields, ?array &$data, bool $isView, array $typeDefinitions): void
private function parseFields(array $fields, array $pks, ?array &$data, array $typeDefinitions): void

Check warning on line 343 in src/Model/Cli/Model/ModelGenerate.php

View check run for this annotation

Codecov / codecov/patch

src/Model/Cli/Model/ModelGenerate.php#L343

Added line #L343 was not covered by tests
{
$idCount = 0;
foreach ($fields as $i => $field)
foreach ($fields as $field)

Check warning on line 345 in src/Model/Cli/Model/ModelGenerate.php

View check run for this annotation

Codecov / codecov/patch

src/Model/Cli/Model/ModelGenerate.php#L345

Added line #L345 was not covered by tests
{
$this->parseFieldType($field['Type'], $typeName, $length, $accuracy, $unsigned);
if ($isView && 0 === $i)
{
$isPk = true;
}
else
{
$isPk = 'PRI' === $field['Key'];
}
$isPk = isset($pks[$field['Field']]);

Check warning on line 348 in src/Model/Cli/Model/ModelGenerate.php

View check run for this annotation

Codecov / codecov/patch

src/Model/Cli/Model/ModelGenerate.php#L348

Added line #L348 was not covered by tests
[$phpType, $phpDefinitionType, $typeConvert] = $this->dbFieldTypeToPhp($typeName);
$data['fields'][] = [
'name' => $field['Field'],
Expand All @@ -365,7 +360,7 @@
'default' => $field['Default'],
'defaultValue' => $this->parseFieldDefaultValue($typeName, $field['Default']),
'isPrimaryKey' => $isPk,
'primaryKeyIndex' => $isPk ? $idCount : -1,
'primaryKeyIndex' => $primaryKeyIndex = ($pks[$field['Field']]['Seq_in_index'] ?? 0) - 1,

Check warning on line 363 in src/Model/Cli/Model/ModelGenerate.php

View check run for this annotation

Codecov / codecov/patch

src/Model/Cli/Model/ModelGenerate.php#L363

Added line #L363 was not covered by tests
'isAutoIncrement' => str_contains($field['Extra'], 'auto_increment'),
'comment' => $field['Comment'],
'typeDefinition' => $typeDefinitions[$field['Field']],
Expand All @@ -375,10 +370,11 @@
];
if ($isPk)
{
$data['table']['id'][] = $field['Field'];
++$idCount;
$data['table']['id'][$primaryKeyIndex] = $field['Field'];

Check warning on line 373 in src/Model/Cli/Model/ModelGenerate.php

View check run for this annotation

Codecov / codecov/patch

src/Model/Cli/Model/ModelGenerate.php#L373

Added line #L373 was not covered by tests
}
}
ksort($data['table']['id']);
$data['table']['id'] = array_values($data['table']['id']);

Check warning on line 377 in src/Model/Cli/Model/ModelGenerate.php

View check run for this annotation

Codecov / codecov/patch

src/Model/Cli/Model/ModelGenerate.php#L376-L377

Added lines #L376 - L377 were not covered by tests
}

/**
Expand Down
Loading