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

[3.0] 废弃模型的一些不合理特性 #601

Merged
merged 9 commits into from
Oct 23, 2023
12 changes: 12 additions & 0 deletions doc/base/version/2.1-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@
### 废弃

* 废弃命名空间声明在 `config.php` 的写法,统一写到 `composer.json` 的 `imi.namespace`

* 废弃支持模型 insert、update 传入 $data 参数

* 废弃模型基类中使用 `ConfigValue` 注解,改为在 `Meta` 类中读取配置

* 废弃按表指定模型生成配置

* 废弃 `Imi\Model\ModelManager`

* 废弃 `Model::updateBatch()` 和 `Model::deleteBatch()`

* 废弃模型查询时动态指定字段名的特殊处理
3 changes: 1 addition & 2 deletions doc/components/orm/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
'Imi\Pgsql\Test\Model\Article' => [
'name' => 'tb_article', // 覆盖注解中定义的表名,还支持:数据库名.表名
'dbPoolName' => null, // 覆盖注解中定义的连接池名
'prefix' => null, // 覆盖注解中定义的表名前缀
],
],
]
```

> 覆盖表名、连接池名的特性,请更新到 imi v2.0.10 及更高版本,重新生成模型基类后有效
58 changes: 0 additions & 58 deletions doc/dev/generate/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,36 +90,6 @@ imi 支持两种自定义模型生成目标的配置方式。
]
```

#### 按表指定-MySQL

项目配置文件:

```php
[
'tools' => [
'generate/model' => [
'relation' => [
'表名1' => [
'namespace' => '生成到的命名空间',
// 是否备份记录,不建议所有表都备份数据,一般用于字典表、默认数据等情况
'withRecords' => true,
'fields' => [
'字段名' => [
'typeDefinition' => false,
],
],
],
'表名2' => [
'namespace' => '生成到的命名空间',
],
],
],
],
]
```

> 即将废弃,推荐使用 `按命名空间指定`

### 生成模型事件-MySQL

在生成模型时,会触发以下事件:
Expand Down Expand Up @@ -297,34 +267,6 @@ vendor/bin/imi-cli generate/pgModel "命名空间"

imi 支持两种自定义模型生成目标的配置方式。

#### 按表指定-PostgreSQL

项目配置文件:

```php
[
'tools' => [
'generate/model' => [
'relation' => [
'表名1' => [
'namespace' => '生成到的命名空间',
// 是否备份记录,不建议所有表都备份数据,一般用于字典表、默认数据等情况
'withRecords' => true,
'fields' => [
'字段名' => [
'typeDefinition' => false,
],
],
],
'表名2' => [
'namespace' => '生成到的命名空间',
],
],
],
],
]
```

#### 按命名空间指定-PostgreSQL

项目配置文件:
Expand Down
25 changes: 0 additions & 25 deletions phpstan-baseline/baseline-core.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,11 @@ parameters:
count: 3
path: ../src/Db/Query/Query.php

-
message: "#^Argument of an invalid type ArrayAccess supplied for foreach, only iterables are supported\\.$#"
count: 1
path: ../src/Model/Model.php

-
message: "#^Left side of && is always true\\.$#"
count: 1
path: ../src/Model/Model.php

-
message: "#^PHPDoc tag @return with type T\\|null is not subtype of native type Imi\\\\Bean\\\\Annotation\\\\Base\\|null\\.$#"
count: 1
path: ../src/Model/ModelManager.php

-
message: "#^Parameter \\#2 \\$class of static method Imi\\\\Server\\\\ServerManager\\:\\:getServer\\(\\) expects class\\-string\\<Imi\\\\Server\\\\Contract\\\\IServer\\>\\|null, string given\\.$#"
count: 1
path: ../src/Server/Group/Listener/GroupRestore.php

-
message: "#^Method Imi\\\\Test\\\\Component\\\\Model\\\\Article\\:\\:getQueryRelationsList\\(\\) should return Imi\\\\Test\\\\Component\\\\Model\\\\Article\\|null but returns Imi\\\\Test\\\\Component\\\\Model\\\\ArticleEx\\|null\\.$#"
count: 1
path: ../tests/unit/Component/Model/Article.php

-
message: "#^Property Imi\\\\Test\\\\Component\\\\Model\\\\Article\\:\\:\\$queryRelationsList \\(Imi\\\\Test\\\\Component\\\\Model\\\\ArticleEx\\|null\\) does not accept Imi\\\\Test\\\\Component\\\\Model\\\\Article\\|null\\.$#"
count: 1
path: ../tests/unit/Component/Model/Article.php

-
message: "#^Argument of an invalid type Imi\\\\Util\\\\ArrayData supplied for foreach, only iterables are supported\\.$#"
count: 1
Expand Down
77 changes: 32 additions & 45 deletions src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,52 +142,34 @@
continue;
}
$className = $this->getClassName($table, $prefix);
if (isset($configData['relation'][$table]))
$hasResult = false;
$fileName = '';
$modelNamespace = '';
$tableConfig = null;
foreach ($configData['namespace'] ?? [] as $namespaceName => $namespaceItem)

Check warning on line 149 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#L145-L149

Added lines #L145 - L149 were not covered by tests
{
$configItem = $configData['relation'][$table];
$modelNamespace = $configItem['namespace'] ?? $namespace;
$path = Imi::getNamespacePath($modelNamespace, true);
if (null === $path)
if (($tableConfig = ($namespaceItem['tables'][$table] ?? null)) || \in_array($table, $namespaceItem['tables'] ?? []))

Check warning on line 151 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#L151

Added line #L151 was not covered by tests
{
$this->output->writeln('<error>Namespace</error> <comment>' . $modelNamespace . '</comment> <error>cannot found</error>');
exit(255);
}
File::createDir($path);
$basePath = $path . '/Base';
File::createDir($basePath);
$fileName = File::path($path, $className . '.php');
}
else
{
$hasResult = false;
$fileName = '';
$modelNamespace = '';
$tableConfig = null;
foreach ($configData['namespace'] ?? [] as $namespaceName => $namespaceItem)
{
if (($tableConfig = ($namespaceItem['tables'][$table] ?? null)) || \in_array($table, $namespaceItem['tables'] ?? []))
$modelNamespace = $namespaceName;
$path = Imi::getNamespacePath($modelNamespace, true);
if (null === $path)

Check warning on line 155 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#L153-L155

Added lines #L153 - L155 were not covered by tests
{
$modelNamespace = $namespaceName;
$path = Imi::getNamespacePath($modelNamespace, true);
if (null === $path)
{
$this->output->writeln('<error>Namespace</error> <comment>' . $modelNamespace . '</comment> <error>cannot found</error>');
exit(255);
}
File::createDir($path);
$basePath = $path . '/Base';
File::createDir($basePath);
$fileName = File::path($path, $className . '.php');
$hasResult = true;
break;
$this->output->writeln('<error>Namespace</error> <comment>' . $modelNamespace . '</comment> <error>cannot found</error>');
exit(255);

Check warning on line 158 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#L157-L158

Added lines #L157 - L158 were not covered by tests
}
File::createDir($path);
$basePath = $path . '/Base';
File::createDir($basePath);
$fileName = File::path($path, $className . '.php');
$hasResult = true;
break;

Check warning on line 165 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#L160-L165

Added lines #L160 - L165 were not covered by tests
}
if (!$hasResult)
{
$modelNamespace = $namespace;
$fileName = File::path($modelPath, $className . '.php');
$basePath = $baseModelPath;
}
}
if (!$hasResult)

Check warning on line 168 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#L168

Added line #L168 was not covered by tests
{
$modelNamespace = $namespace;
$fileName = File::path($modelPath, $className . '.php');
$basePath = $baseModelPath;

Check warning on line 172 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#L170-L172

Added lines #L170 - L172 were not covered by tests
}
if (false === $override && is_file($fileName))
{
Expand Down Expand Up @@ -236,7 +218,12 @@
order by attnum
;
SQL)->getArray();
$this->parseFields($poolName, $fields, $data, 'v' === $item['relkind'], $table, $configData);
$typeDefinitions = [];
foreach ($fields as $field)

Check warning on line 222 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#L221-L222

Added lines #L221 - L222 were not covered by tests
{
$typeDefinitions[$field['attname']] = ($tableConfig['fields'][$field['attname']]['typeDefinition'] ?? null) ?? true;

Check warning on line 224 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#L224

Added line #L224 was not covered by tests
}
$this->parseFields($poolName, $fields, $data, 'v' === $item['relkind'], $table, $typeDefinitions);

Check warning on line 226 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#L226

Added line #L226 was not covered by tests

$baseFileName = File::path($basePath, $className . 'Base.php');
if (!is_file($baseFileName) || true === $override || 'base' === $override)
Expand Down Expand Up @@ -290,7 +277,7 @@
/**
* 处理字段信息.
*/
private function parseFields(?string $poolName, array $fields, ?array &$data, bool $isView, string $table, ?array $config): void
private function parseFields(?string $poolName, array $fields, ?array &$data, bool $isView, string $table, ?array $typeDefinitions): void

Check warning on line 280 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#L280

Added line #L280 was not covered by tests
{
foreach ($fields as $field)
{
Expand Down Expand Up @@ -334,7 +321,7 @@
'primaryKeyIndex' => $primaryKeyIndex = ($field['ordinal_position'] ?? 0) - 1,
'isAutoIncrement' => '' !== $field['attidentity'],
'comment' => $field['description'] ?? '',
'typeDefinition' => $config['relation'][$table]['fields'][$field['attname']]['typeDefinition'] ?? true,
'typeDefinition' => $typeDefinitions[$field['attname']],

Check warning on line 324 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#L324

Added line #L324 was not covered by tests
'ref' => \in_array($type, ['json', 'jsonb']),
'virtual' => 's' === $field['attgenerated'],
];
Expand Down Expand Up @@ -378,7 +365,7 @@
'double' => ['float', '?float', '(float)'],
'float4' => ['float', '?float', '(float)'],
'float8' => ['float', '?float', '(float)'],
'numeric' => ['string|float|int', \PHP_VERSION_ID >= 80000 ? 'string|float|int|null' : '', ''],
'numeric' => ['string|float|int', 'string|float|int|null', ''],
'json' => ['\\' . \Imi\Util\LazyArrayObject::class . '|array', '', ''],
'jsonb' => ['\\' . \Imi\Util\LazyArrayObject::class . '|array', '', ''],
];
Expand Down
11 changes: 7 additions & 4 deletions src/Components/pgsql/src/Model/Cli/Model/base-template.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ declare(strict_types=1);

namespace <?php echo $namespace; ?>\Base;

use Imi\Config\Annotation\ConfigValue;
use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\Entity;
use Imi\Model\Annotation\Table;
Expand All @@ -16,9 +15,13 @@ use <?php echo $baseClassName; ?> as Model;
* <?php echo $tableComment; ?> 基类.
*
* @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 }?>))
* @Table(name="<?php echo $table['name']; ?>", usePrefix=<?php var_export($table['usePrefix']); ?><?php if (isset($table['id'][0]))
{ ?>, id={<?php echo '"', implode('", "', $table['id']), '"'; ?>}<?php } ?>, dbPoolName=<?php if (null === $poolName)
{ ?>null<?php }
else
{
echo '"', $poolName, '"';
}?>)
*
<?php foreach ($fields as $field)
{ ?>
Expand Down
3 changes: 1 addition & 2 deletions src/Components/pgsql/tests/Model/Base/ArrayTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Imi\Pgsql\Test\Model\Base;

use Imi\Config\Annotation\ConfigValue;
use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\Entity;
use Imi\Model\Annotation\Table;
Expand All @@ -15,7 +14,7 @@
*
* @Entity(camel=true, bean=true, incrUpdate=false)
*
* @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\ArrayTest.name", default="tb_array_test"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\ArrayTest.poolName"))
* @Table(name="tb_array_test", usePrefix=false, id={"id"}, dbPoolName=null)
*
* @property int|null $id
* @property array<int>|null $arr1
Expand Down
3 changes: 1 addition & 2 deletions src/Components/pgsql/tests/Model/Base/ArticleBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Imi\Pgsql\Test\Model\Base;

use Imi\Config\Annotation\ConfigValue;
use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\Entity;
use Imi\Model\Annotation\Table;
Expand All @@ -15,7 +14,7 @@
*
* @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"))
* @Table(name="tb_article", usePrefix=false, id={"id"}, dbPoolName=null)
*
* @property int|null $id
* @property string|null $title
Expand Down
3 changes: 1 addition & 2 deletions src/Components/pgsql/tests/Model/Base/MemberBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Imi\Pgsql\Test\Model\Base;

use Imi\Config\Annotation\ConfigValue;
use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\Entity;
use Imi\Model\Annotation\Table;
Expand All @@ -15,7 +14,7 @@
*
* @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"))
* @Table(name="tb_member", usePrefix=false, id={"id"}, dbPoolName=null)
*
* @property int|null $id
* @property string|null $username 用户名
Expand Down
3 changes: 1 addition & 2 deletions src/Components/pgsql/tests/Model/Base/NoIncPkBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Imi\Pgsql\Test\Model\Base;

use Imi\Config\Annotation\ConfigValue;
use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\Entity;
use Imi\Model\Annotation\Table;
Expand All @@ -15,7 +14,7 @@
*
* @Entity(camel=true, bean=true, incrUpdate=false)
*
* @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\NoIncPk.name", default="tb_no_inc_pk"), usePrefix=false, id={"a_id", "b_id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\NoIncPk.poolName"))
* @Table(name="tb_no_inc_pk", usePrefix=false, id={"a_id", "b_id"}, dbPoolName=null)
*
* @property int|null $aId
* @property int|null $bId
Expand Down
3 changes: 1 addition & 2 deletions src/Components/pgsql/tests/Model/Base/PerformanceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Imi\Pgsql\Test\Model\Base;

use Imi\Config\Annotation\ConfigValue;
use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\Entity;
use Imi\Model\Annotation\Table;
Expand All @@ -15,7 +14,7 @@
*
* @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"))
* @Table(name="tb_performance", usePrefix=false, id={"id"}, dbPoolName=null)
*
* @property int|null $id
* @property string|null $value
Expand Down
3 changes: 1 addition & 2 deletions src/Components/pgsql/tests/Model/Base/TestJsonBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Imi\Pgsql\Test\Model\Base;

use Imi\Config\Annotation\ConfigValue;
use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\Entity;
use Imi\Model\Annotation\Table;
Expand All @@ -15,7 +14,7 @@
*
* @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"))
* @Table(name="tb_test_json", usePrefix=false, id={"id"}, dbPoolName=null)
*
* @property int|null $id
* @property \Imi\Util\LazyArrayObject|array|null $jsonData json数据
Expand Down
3 changes: 1 addition & 2 deletions src/Components/pgsql/tests/Model/Base/TestSoftDeleteBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Imi\Pgsql\Test\Model\Base;

use Imi\Config\Annotation\ConfigValue;
use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\Entity;
use Imi\Model\Annotation\Table;
Expand All @@ -15,7 +14,7 @@
*
* @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"))
* @Table(name="tb_test_soft_delete", usePrefix=false, id={"id"}, dbPoolName=null)
*
* @property int|null $id
* @property string|null $title
Expand Down
Loading
Loading