Skip to content

Commit

Permalink
支持 PostgreSQL 生成模型配置 bean 和 incrUpdate 参数 (imiphp#524)
Browse files Browse the repository at this point in the history
* 优化代码

* 支持 PostgreSQL 生成模型配置 bean 和 incrUpdate 参数

* 生成测试模型

* 修复
  • Loading branch information
Yurunsoft authored and NHZEX committed Oct 18, 2023
1 parent 6947585 commit cbace48
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 34 deletions.
9 changes: 7 additions & 2 deletions src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ class ModelGenerate extends BaseCommand
* @Option(name="basePath", type=ArgType::STRING, default=null, comments="指定命名空间对应的基准路径,可选")
* @Option(name="entity", type=ArgType::BOOLEAN, default=true, comments="序列化时是否使用驼峰命名(true or false),默认true,可选")
* @Option(name="lengthCheck", type=ArgType::BOOLEAN, default=false, comments="是否检查字符串字段长度,可选")
* @Option(name="bean", type=ArgType::BOOL, comments="模型对象是否作为 bean 类使用", default=true)
* @Option(name="incrUpdate", type=ArgType::BOOL, comments="模型是否启用增量更新", default=false)
*
* @param string|bool $override
* @param string|bool $config
*/
public function generate(string $namespace, string $baseClass, ?string $database, ?string $poolName, array $prefix, array $include, array $exclude, $override, $config, ?string $basePath, bool $entity, bool $lengthCheck): void
public function generate(string $namespace, string $baseClass, ?string $database, ?string $poolName, array $prefix, array $include, array $exclude, $override, $config, ?string $basePath, bool $entity, bool $lengthCheck, bool $bean, bool $incrUpdate): void
{
$db = Db::getInstance($poolName);
$tablePrefix = $db->getOption()['prefix'] ?? '';
Expand Down Expand Up @@ -161,9 +163,10 @@ public function generate(string $namespace, string $baseClass, ?string $database
$hasResult = false;
$fileName = '';
$modelNamespace = '';
$tableConfig = null;
foreach ($configData['namespace'] ?? [] as $namespaceName => $namespaceItem)
{
if (\in_array($table, $namespaceItem['tables'] ?? []))
if (($tableConfig = ($namespaceItem['tables'][$table] ?? null)) || \in_array($table, $namespaceItem['tables'] ?? []))
{
$modelNamespace = $namespaceName;
$path = Imi::getNamespacePath($modelNamespace, true);
Expand Down Expand Up @@ -217,6 +220,8 @@ public function generate(string $namespace, string $baseClass, ?string $database
],
'fields' => [],
'entity' => $entity,
'bean' => $tableConfig['bean'] ?? $bean,
'incrUpdate' => $tableConfig['incrUpdate'] ?? $incrUpdate,
'poolName' => $poolName,
'tableComment' => $tableComment,
'lengthCheck' => $lengthCheck,
Expand Down
32 changes: 19 additions & 13 deletions src/Components/pgsql/src/Model/Cli/Model/base-template.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ use <?php echo $baseClassName; ?> as Model;
/**
* <?php echo $tableComment; ?> 基类.
*
* <?php if (true === $entity) { ?>@Entity<?php }
else
{ ?>@Entity(false)<?php } ?>

* @Table(name=@ConfigValue(name="@app.models.<?php echo $namespace; ?>\<?php echo $className; ?>.name", default="<?php echo $table['name']; ?>"), usePrefix=<?php var_export($table['usePrefix']); ?><?php if (isset($table['id'][0])) { ?>, id={<?php echo '"', implode('", "', $table['id']), '"'; ?>}<?php } ?>, dbPoolName=@ConfigValue(name="@app.models.<?php echo $namespace; ?>\<?php echo $className; ?>.poolName"<?php if (null !== $poolName) {?>, default="<?php echo $poolName; ?>"<?php }?>))
* @Entity(camel=<?php echo var_export($entity, true); ?>, bean=<?php echo var_export($bean, true); ?>, incrUpdate=<?php echo var_export($incrUpdate, true); ?>)
* @Table(name=@ConfigValue(name="@app.models.<?php echo $namespace; ?>\<?php echo $className; ?>.name", default="<?php echo $table['name']; ?>"), usePrefix=<?php var_export($table['usePrefix']); ?><?php if (isset($table['id'][0]))
{ ?>, id={<?php echo '"', implode('", "', $table['id']), '"'; ?>}<?php } ?>, dbPoolName=@ConfigValue(name="@app.models.<?php echo $namespace; ?>\<?php echo $className; ?>.poolName"<?php if (null !== $poolName)
{?>, default="<?php echo $poolName; ?>"<?php }?>))
*
<?php foreach ($fields as $field) { ?>
<?php foreach ($fields as $field)
{ ?>
* @property <?php echo $field['phpType']; ?> $<?php echo $field['varName']; ?> <?php echo '' === $field['comment'] ? '' : $field['comment']; ?>
<?php } ?>
Expand All @@ -43,11 +43,12 @@ abstract class <?php echo $className; ?>Base extends Model
{
?>
/**
<?php if ('' === $field['comment']) { ?>
<?php if ('' === $field['comment'])
{ ?>
* <?php echo $field['name']; ?>.
<?php }
else
{ ?>
else
{ ?>
* <?php echo $field['comment']; ?>.
* <?php echo $field['name']; ?>
Expand All @@ -57,15 +58,18 @@ abstract class <?php echo $className; ?>Base extends Model
* @var <?php echo $field['phpType']; ?>

*/
protected <?php if ($field['typeDefinition'] && $field['phpDefinitionType']) { ?><?php echo $field['phpDefinitionType']; ?> <?php } ?>$<?php echo $field['varName']; ?> = <?php var_export($field['defaultValue']); ?>;
protected <?php if ($field['typeDefinition'] && $field['phpDefinitionType'])
{ ?><?php echo $field['phpDefinitionType']; ?> <?php } ?>$<?php echo $field['varName']; ?> = <?php var_export($field['defaultValue']); ?>;

/**
* 获取 <?php echo $field['varName']; ?><?php echo '' === $field['comment'] ? '' : (' - ' . $field['comment']); ?>.
*
* @return <?php echo $field['phpType']; ?>

*/
public function <?php if ($field['ref']){?> & <?php } ?>get<?php echo ucfirst($field['varName']); ?>()<?php if ($field['typeDefinition'] && $field['phpDefinitionType']) { ?>: <?php echo $field['phpDefinitionType']; ?><?php } ?>
public function <?php if ($field['ref'])
{?> & <?php } ?>get<?php echo ucfirst($field['varName']); ?>()<?php if ($field['typeDefinition'] && $field['phpDefinitionType'])
{ ?>: <?php echo $field['phpDefinitionType']; ?><?php } ?>

{
return $this-><?php echo $field['varName']; ?>;
Expand All @@ -78,7 +82,8 @@ abstract class <?php echo $className; ?>Base extends Model

* @return static
*/
public function set<?php echo ucfirst($field['varName']); ?>(<?php if ($field['typeDefinition'] && $field['phpDefinitionType']) { ?><?php echo $field['phpDefinitionType']; ?> <?php } ?>$<?php echo $field['varName']; ?>)
public function set<?php echo ucfirst($field['varName']); ?>(<?php if ($field['typeDefinition'] && $field['phpDefinitionType'])
{ ?><?php echo $field['phpDefinitionType']; ?> <?php } ?>$<?php echo $field['varName']; ?>)
{
<?php if ($lengthCheck && $length = [
'character' => $field['length'],
Expand All @@ -88,7 +93,8 @@ abstract class <?php echo $className; ?>Base extends Model
'bit' => $field['length'],
'bit varying' => $field['length'],
'varbit' => $field['length'],
][$field['type']] ?? null) { ?>
][$field['type']] ?? null)
{ ?>
if (is_string($<?php echo $field['varName']; ?>) && mb_strlen($<?php echo $field['varName']; ?>) > <?php echo $length; ?>)
{
throw new \InvalidArgumentException('The maximum length of $<?php echo $field['varName']; ?> is <?php echo $length; ?>');
Expand Down
9 changes: 0 additions & 9 deletions src/Components/pgsql/src/Model/PgModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Imi\Pgsql\Model;

use Imi\Model\Contract\IModelQuery;
use Imi\Model\Model;

/**
Expand All @@ -14,14 +13,6 @@ class PgModel extends Model
{
public const DEFAULT_QUERY_CLASS = ModelQuery::class;

/**
* {@inheritDoc}
*/
public static function query(?string $poolName = null, ?int $queryType = null, ?string $queryClass = null, ?string $alias = null): IModelQuery
{
return parent::query($poolName, $queryType, $queryClass, $alias);
}

/**
* @param bool|int $timeAccuracy
*
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 @@ -13,7 +13,7 @@
/**
* tb_article 基类.
*
* @Entity
* @Entity(camel=true, bean=true, incrUpdate=false)
*
* @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\Article.name", default="tb_article"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\Article.poolName"))
*
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 @@ -13,7 +13,7 @@
/**
* tb_member 基类.
*
* @Entity
* @Entity(camel=true, bean=true, incrUpdate=false)
*
* @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\Member.name", default="tb_member"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\Member.poolName"))
*
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 @@ -13,7 +13,7 @@
/**
* tb_performance 基类.
*
* @Entity
* @Entity(camel=true, bean=true, incrUpdate=false)
*
* @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\Performance.name", default="tb_performance"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\Performance.poolName"))
*
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 @@ -13,7 +13,7 @@
/**
* test 基类.
*
* @Entity
* @Entity(camel=true, bean=true, incrUpdate=false)
*
* @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\TestJson.name", default="tb_test_json"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\TestJson.poolName"))
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* tb_test_soft_delete 基类.
*
* @Entity
* @Entity(camel=true, bean=true, incrUpdate=false)
*
* @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\TestSoftDelete.name", default="tb_test_soft_delete"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\TestSoftDelete.poolName"))
*
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 @@ -13,7 +13,7 @@
/**
* tb_tree 基类.
*
* @Entity
* @Entity(camel=true, bean=true, incrUpdate=false)
*
* @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\Tree.name", default="tb_tree"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\Tree.poolName"))
*
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 @@ -13,7 +13,7 @@
/**
* tb_update_time 基类.
*
* @Entity
* @Entity(camel=true, bean=true, incrUpdate=false)
*
* @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\UpdateTime.name", default="tb_update_time"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\UpdateTime.poolName"))
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* tb_virtual_column 基类.
*
* @Entity
* @Entity(camel=true, bean=true, incrUpdate=false)
*
* @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\VirtualColumn.name", default="tb_virtual_column"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\VirtualColumn.poolName"))
*
Expand Down
4 changes: 2 additions & 2 deletions src/Model/SoftDelete/Traits/TSoftDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ public static function query(?string $poolName = null, ?int $queryType = null, ?
* @param string|null $poolName 连接池名,为null则取默认
* @param int|null $queryType 查询类型;Imi\Db\Query\QueryType::READ/WRITE
*/
public static function originQuery(?string $poolName = null, ?int $queryType = null, string $queryClass = self::DEFAULT_QUERY_CLASS): IModelQuery
public static function originQuery(?string $poolName = null, ?int $queryType = null, ?string $queryClass = null, ?string $alias = null): IModelQuery
{
return parent::query($poolName, $queryType, $queryClass);
return parent::query($poolName, $queryType, $queryClass, $alias);
}

/**
Expand Down

0 comments on commit cbace48

Please sign in to comment.