Skip to content

Commit

Permalink
修复有多个主键的表在生成模型时,主键顺序可能不正确 (imiphp#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft authored and NHZEX committed Oct 18, 2023
1 parent 7c9a7dd commit 55b74ae
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 29 deletions.
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 getClassName(string $table, array $prefixs): string
*/
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 @@ private function parseFields(?string $poolName, array $fields, ?array &$data, bo
'default' => $field['adsrc'],
'defaultValue' => $this->parseFieldDefaultValue($poolName, $type, $field['adsrc']),
'isPrimaryKey' => $isPk,
'primaryKeyIndex' => $field['ordinal_position'] ?? -1,
'primaryKeyIndex' => $primaryKeyIndex = ($field['ordinal_position'] ?? 0) - 1,
'isAutoIncrement' => '' !== $field['attidentity'],
'comment' => $field['description'] ?? '',
'typeDefinition' => $config['relation'][$table]['fields'][$field['attname']]['typeDefinition'] ?? true,
Expand All @@ -340,10 +339,11 @@ private function parseFields(?string $poolName, array $fields, ?array &$data, bo
];
if ($isPk)
{
$data['table']['id'][] = $field['attname'];
++$idCount;
$data['table']['id'][$primaryKeyIndex] = $field['attname'];
}
}
ksort($data['table']['id']);
$data['table']['id'] = array_values($data['table']['id']);
}

/**
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 @@ public function generate(string $namespace, string $baseClass, ?string $database
{
$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');

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

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

Expand Down Expand Up @@ -337,20 +340,12 @@ private function getClassName(string $table, array $prefixs): string
/**
* 处理字段信息.
*/
private function parseFields(array $fields, ?array &$data, bool $isView, array $typeDefinitions): void
private function parseFields(array $fields, array $pks, ?array &$data, array $typeDefinitions): void
{
$idCount = 0;
foreach ($fields as $i => $field)
foreach ($fields as $field)
{
$this->parseFieldType($field['Type'], $typeName, $length, $accuracy, $unsigned);
if ($isView && 0 === $i)
{
$isPk = true;
}
else
{
$isPk = 'PRI' === $field['Key'];
}
$isPk = isset($pks[$field['Field']]);
[$phpType, $phpDefinitionType, $typeConvert] = $this->dbFieldTypeToPhp($typeName);
$data['fields'][] = [
'name' => $field['Field'],
Expand All @@ -365,7 +360,7 @@ private function parseFields(array $fields, ?array &$data, bool $isView, array $
'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,
'isAutoIncrement' => str_contains($field['Extra'], 'auto_increment'),
'comment' => $field['Comment'],
'typeDefinition' => $typeDefinitions[$field['Field']],
Expand All @@ -375,10 +370,11 @@ private function parseFields(array $fields, ?array &$data, bool $isView, array $
];
if ($isPk)
{
$data['table']['id'][] = $field['Field'];
++$idCount;
$data['table']['id'][$primaryKeyIndex] = $field['Field'];
}
}
ksort($data['table']['id']);
$data['table']['id'] = array_values($data['table']['id']);
}

/**
Expand Down

0 comments on commit 55b74ae

Please sign in to comment.