Skip to content

Commit

Permalink
新增模型主键名常量 PRIMARY_KEY、PRIMARY_KEYS (#359)
Browse files Browse the repository at this point in the history
* 新增模型主键名常量 PRIMARY_KEY、PRIMARY_KEYS

* 更新测试

* 更新文档
  • Loading branch information
Yurunsoft authored Jun 24, 2022
1 parent 7dd31b6 commit ff8bfc1
Show file tree
Hide file tree
Showing 28 changed files with 253 additions and 16 deletions.
4 changes: 2 additions & 2 deletions doc/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@
* [数据库表模型](components/orm/RDModel.md)
* [模型配置](components/orm/config.md)
* [模型定义](components/orm/RDModel/definition.md)
* [模型方法](components/orm/RDModel.md)
* [模型生成](dev/generate/model.md)
* [表生成](dev/generate/table.md)
* [模型用法](components/orm/RDModel.md)
* [从模型生成表](dev/generate/table.md)
* [模型事件](components/orm/RDModel/event.md)
* [树形表模型](components/orm/TreeModel.md)
* [模型软删除](components/orm/softDelete.md)
Expand Down
8 changes: 7 additions & 1 deletion doc/components/orm/RDModel.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# 数据库表模型
# 模型用法

[toc]

## 模型常量

`TestModel::PRIMARY_KEY` 第一个主键名称,字符串

`TestModel::PRIMARY_KEYS` 主键名称数组

## 模型操作

### 初始化模型
Expand Down
2 changes: 1 addition & 1 deletion doc/dev/generate/table.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 表生成
# 从模型生成表

[toc]

Expand Down
10 changes: 10 additions & 0 deletions src/Components/pgsql/src/Model/Cli/Model/base-template.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ else
*/
abstract class <?php echo $className; ?>Base extends Model
{
/**
* {@inheritdoc}
*/
public const PRIMARY_KEY = '<?php echo addcslashes($table['id'][0] ?? '', '\'\\'); ?>';

/**
* {@inheritdoc}
*/
public const PRIMARY_KEYS = <?php echo json_encode($table['id'], JSON_UNESCAPED_UNICODE); ?>;

<?php
foreach ($fields as $field)
{
Expand Down
10 changes: 10 additions & 0 deletions src/Model/Cli/Model/base-template.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ use <?php echo $baseClassName; ?> as Model;
*/
abstract class <?php echo $className; ?>Base extends Model
{
/**
* {@inheritdoc}
*/
public const PRIMARY_KEY = '<?php echo addcslashes($table['id'][0] ?? '', '\'\\'); ?>';

/**
* {@inheritdoc}
*/
public const PRIMARY_KEYS = <?php echo json_encode($table['id'], \JSON_UNESCAPED_UNICODE); ?>;

<?php
foreach ($fields as $field)
{
Expand Down
26 changes: 20 additions & 6 deletions src/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ abstract class Model extends BaseModel
{
public const DEFAULT_QUERY_CLASS = ModelQuery::class;

/**
* 第一个主键名称.
*
* @var string|null
*/
public const PRIMARY_KEY = null;

/**
* 主键名称数组.
*
* @var string[]|null
*/
public const PRIMARY_KEYS = null;

/**
* 动态模型集合.
*/
Expand Down Expand Up @@ -233,7 +247,7 @@ public static function exists(...$ids): bool
else
{
// 主键值
foreach (static::__getMeta()->getId() as $i => $name)
foreach (static::PRIMARY_KEYS ?? static::__getMeta()->getId() as $i => $name)
{
if (!isset($ids[$i]))
{
Expand Down Expand Up @@ -280,7 +294,7 @@ public static function find(...$ids): ?self
else
{
// 主键值
foreach (static::__getMeta()->getId() as $i => $name)
foreach (static::PRIMARY_KEYS ?? static::__getMeta()->getId() as $i => $name)
{
if (!isset($ids[$i]))
{
Expand Down Expand Up @@ -424,7 +438,7 @@ public function update($data = null): IResult
}

$hasIdWhere = false;
foreach ($meta->getId() as $idName)
foreach (static::PRIMARY_KEYS ?? $meta->getId() as $idName)
{
if (isset($data[$idName]))
{
Expand Down Expand Up @@ -570,7 +584,7 @@ public function save(): IResult
}
else
{
foreach ($meta->getId() as $idName)
foreach (static::PRIMARY_KEYS ?? $meta->getId() as $idName)
{
if (isset($data[$idName]))
{
Expand Down Expand Up @@ -621,7 +635,7 @@ public function delete(): IResult
}

$hasIdWhere = false;
foreach ($meta->getId() as $idName)
foreach (static::PRIMARY_KEYS ?? $meta->getId() as $idName)
{
if (isset($this[$idName]))
{
Expand Down Expand Up @@ -1075,7 +1089,7 @@ private static function parseSaveData($data, string $type, ?self $object = null)
// 更新时无需更新主键
if ($isUpdate)
{
foreach ($meta->getId() as $id)
foreach (static::PRIMARY_KEYS ?? $meta->getId() as $id)
{
if (isset($result[$id]))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Relation/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public static function parseByOneToMany(Model $model, string $propertyName, \Imi
if ($orphanRemoval)
{
// 删除无关联数据
$pks = $rightModel::__getMeta()->getId();
$pks = $rightModel::PRIMARY_KEYS ?? $rightModel::__getMeta()->getId();
if (isset($pks[1]))
{
throw new \RuntimeException(sprintf('%s can not OneToMany, because has more than 1 primary keys', $rightModel));
Expand Down Expand Up @@ -492,7 +492,7 @@ public static function parseByPolymorphicOneToMany(Model $model, string $propert
if ($orphanRemoval)
{
// 删除无关联数据
$pks = $rightModel::__getMeta()->getId();
$pks = $rightModel::PRIMARY_KEYS ?? $rightModel::__getMeta()->getId();
if (isset($pks[1]))
{
throw new \RuntimeException(sprintf('%s can not OneToMany, because has more than 1 primary keys', $rightModel));
Expand Down
6 changes: 3 additions & 3 deletions src/Model/SoftDelete/Traits/TSoftDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function delete(): IResult
], $this, \Imi\Model\Event\Param\BeforeDeleteEventParam::class);
}

$id = $meta->getId();
$id = static::PRIMARY_KEYS ?? $meta->getId();
if ($id)
{
foreach ($id as $idName)
Expand Down Expand Up @@ -206,7 +206,7 @@ public static function findDeleted(...$ids): ?Model
else
{
// 主键值
foreach (static::__getMeta()->getId() as $i => $name)
foreach (static::PRIMARY_KEYS ?? static::__getMeta()->getId() as $i => $name)
{
if (!isset($ids[$i]))
{
Expand Down Expand Up @@ -244,7 +244,7 @@ public function restore(): IResult
/** @var IQuery $query */
$query = static::dbQuery();
$meta = $this->__meta;
$id = $meta->getId();
$id = static::PRIMARY_KEYS ?? $meta->getId();
if ($id)
{
foreach ($id as $idName)
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/Component/Model/Base/Article2Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
*/
abstract class Article2Base extends Model
{
/**
* {@inheritdoc}
*/
public const PRIMARY_KEY = 'id';

/**
* {@inheritdoc}
*/
public const PRIMARY_KEYS = ['id'];

/**
* id.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/Component/Model/Base/ArticleBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
*/
abstract class ArticleBase extends Model
{
/**
* {@inheritdoc}
*/
public const PRIMARY_KEY = 'id';

/**
* {@inheritdoc}
*/
public const PRIMARY_KEYS = ['id'];

/**
* id.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/Component/Model/Base/ArticleExBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
*/
abstract class ArticleExBase extends Model
{
/**
* {@inheritdoc}
*/
public const PRIMARY_KEY = 'article_id';

/**
* {@inheritdoc}
*/
public const PRIMARY_KEYS = ['article_id'];

/**
* article_id.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/Component/Model/Base/CreateTimeBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
*/
abstract class CreateTimeBase extends Model
{
/**
* {@inheritdoc}
*/
public const PRIMARY_KEY = 'id';

/**
* {@inheritdoc}
*/
public const PRIMARY_KEYS = ['id'];

/**
* id.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/Component/Model/Base/MemberBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
*/
abstract class MemberBase extends Model
{
/**
* {@inheritdoc}
*/
public const PRIMARY_KEY = 'id';

/**
* {@inheritdoc}
*/
public const PRIMARY_KEYS = ['id'];

/**
* id.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/Component/Model/Base/MemberRoleRelationBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
*/
abstract class MemberRoleRelationBase extends Model
{
/**
* {@inheritdoc}
*/
public const PRIMARY_KEY = 'id';

/**
* {@inheritdoc}
*/
public const PRIMARY_KEYS = ['id'];

/**
* id.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/Component/Model/Base/PerformanceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
*/
abstract class PerformanceBase extends Model
{
/**
* {@inheritdoc}
*/
public const PRIMARY_KEY = 'id';

/**
* {@inheritdoc}
*/
public const PRIMARY_KEYS = ['id'];

/**
* id.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/Component/Model/Base/PolymorphicBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
*/
abstract class PolymorphicBase extends Model
{
/**
* {@inheritdoc}
*/
public const PRIMARY_KEY = 'id';

/**
* {@inheritdoc}
*/
public const PRIMARY_KEYS = ['id'];

/**
* id.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/Component/Model/Base/PrefixBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
*/
abstract class PrefixBase extends Model
{
/**
* {@inheritdoc}
*/
public const PRIMARY_KEY = 'id';

/**
* {@inheritdoc}
*/
public const PRIMARY_KEYS = ['id'];

/**
* id.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/Component/Model/Base/RoleBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
*/
abstract class RoleBase extends Model
{
/**
* {@inheritdoc}
*/
public const PRIMARY_KEY = 'id';

/**
* {@inheritdoc}
*/
public const PRIMARY_KEYS = ['id'];

/**
* id.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/Component/Model/Base/TestEnumBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
*/
abstract class TestEnumBase extends Model
{
/**
* {@inheritdoc}
*/
public const PRIMARY_KEY = 'id';

/**
* {@inheritdoc}
*/
public const PRIMARY_KEYS = ['id'];

/**
* id.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/Component/Model/Base/TestJsonBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
*/
abstract class TestJsonBase extends Model
{
/**
* {@inheritdoc}
*/
public const PRIMARY_KEY = 'id';

/**
* {@inheritdoc}
*/
public const PRIMARY_KEYS = ['id'];

/**
* id.
*
Expand Down
Loading

0 comments on commit ff8bfc1

Please sign in to comment.