mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-30 02:37:58 +08:00
Optimized code.
This commit is contained in:
parent
79d33da19e
commit
8fc0a557d5
@ -7,7 +7,7 @@
|
||||
|
||||
## Changed
|
||||
|
||||
- [#1384](https://github.com/hyperf/hyperf/pull/1384) add `gen:model --property-case` configure. properties as camel
|
||||
- [#1384](https://github.com/hyperf/hyperf/pull/1384) Added option `property-case` for command `gen:model`.
|
||||
|
||||
## Fixed
|
||||
|
||||
|
@ -30,16 +30,21 @@ $ php bin/hyperf.php db:model table_name
|
||||
| --table-mapping | array | `[]` | 为表名 -> 模型增加映射关系 比如 ['users:Account'] |
|
||||
| --ignore-tables | array | `[]` | 不需要生成模型的表名 比如 ['users'] |
|
||||
| --with-comments | bool | `false` | 是否增加字段注释 |
|
||||
| --property-case | bool | `false` | 是否将表字段转为驼峰式 |
|
||||
| --property-case | int | `0` | 字段类型 0蛇形 1驼峰 |
|
||||
|
||||
当使用 `--property-case` 将字段类型转化为驼峰时,还需要手动在模型中加入 `Hyperf\Database\Model\Concerns\CamelCase`。
|
||||
|
||||
对应配置也可以配置到 `databases.{pool}.commands.gen:model` 中,如下
|
||||
|
||||
> 中划线都需要转化为下划线
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Hyperf\Database\Commands\ModelOption;
|
||||
|
||||
return [
|
||||
'default' => [
|
||||
// 忽略其他配置
|
||||
@ -51,9 +56,8 @@ return [
|
||||
'uses' => '',
|
||||
'refresh_fillable' => true,
|
||||
'table_mapping' => [],
|
||||
// 注意这里都是下划线
|
||||
'with_comments' => true,
|
||||
'property_case' => true,
|
||||
'property_case' => ModelOption::PROPERTY_SNAKE_CASE,
|
||||
],
|
||||
],
|
||||
],
|
||||
|
@ -94,7 +94,7 @@ class ModelUpdateVisitor extends NodeVisitorAbstract
|
||||
|
||||
protected function getProperty($column): array
|
||||
{
|
||||
$name = $this->option->isPropertyCase() ? Str::camel($column['column_name']) : $column['column_name'];
|
||||
$name = $this->option->isCamelCase() ? Str::camel($column['column_name']) : $column['column_name'];
|
||||
|
||||
$type = $this->formatPropertyType($column['data_type'], $column['cast'] ?? null);
|
||||
|
||||
|
@ -100,8 +100,7 @@ class ModelCommand extends Command
|
||||
->setIgnoreTables($this->getOption('ignore-tables', 'commands.gen:model.ignore_tables', $pool, []))
|
||||
->setWithComments($this->getOption('with-comments', 'commands.gen:model.with_comments', $pool, false))
|
||||
->setVisitors($this->getOption('visitors', 'commands.gen:model.visitors', $pool, []))
|
||||
->setPropertyCase($this->getOption("property-case","commands.gen:model.property_case",$pool,false));
|
||||
|
||||
->setPropertyCase($this->getOption('property-case', 'commands.gen:model.property_case', $pool));
|
||||
|
||||
if ($table) {
|
||||
$this->createModel($table, $option);
|
||||
@ -125,8 +124,7 @@ class ModelCommand extends Command
|
||||
$this->addOption('ignore-tables', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Ignore tables for creating models.');
|
||||
$this->addOption('with-comments', null, InputOption::VALUE_NONE, 'Whether generate the property comments for model.');
|
||||
$this->addOption('visitors', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Custom visitors for ast traverser.');
|
||||
$this->addOPtion('property-case',null,InputOption::VALUE_NONE,'properties as camel case, instead of snake case.');
|
||||
|
||||
$this->addOption('property-case', null, InputOption::VALUE_OPTIONAL, 'Which property case you want use, 0: snake case, 1: camel case.');
|
||||
}
|
||||
|
||||
protected function getSchemaBuilder(string $poolName): MySqlBuilder
|
||||
@ -238,7 +236,7 @@ class ModelCommand extends Command
|
||||
{
|
||||
$result = $this->input->getOption($name);
|
||||
$nonInput = null;
|
||||
if (in_array($name, ['force-casts', 'refresh-fillable', 'with-comments','property-case'])) {
|
||||
if (in_array($name, ['force-casts', 'refresh-fillable', 'with-comments'])) {
|
||||
$nonInput = false;
|
||||
}
|
||||
if (in_array($name, ['table-mapping', 'ignore-tables', 'visitors'])) {
|
||||
|
@ -14,6 +14,10 @@ namespace Hyperf\Database\Commands;
|
||||
|
||||
class ModelOption
|
||||
{
|
||||
const PROPERTY_SNAKE_CASE = 0;
|
||||
|
||||
const PROPERTY_CAMEL_CASE = 1;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
@ -70,9 +74,9 @@ class ModelOption
|
||||
protected $visitors = [];
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
* @var int
|
||||
*/
|
||||
protected $propertyCase;
|
||||
protected $propertyCase = self::PROPERTY_SNAKE_CASE;
|
||||
|
||||
public function getPool(): string
|
||||
{
|
||||
@ -199,14 +203,14 @@ class ModelOption
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isPropertyCase(): bool
|
||||
public function isCamelCase(): bool
|
||||
{
|
||||
return $this->propertyCase;
|
||||
return $this->propertyCase === self::PROPERTY_CAMEL_CASE;
|
||||
}
|
||||
|
||||
public function setPropertyCase(bool $propertyCase): self
|
||||
public function setPropertyCase($propertyCase): self
|
||||
{
|
||||
$this->propertyCase = $propertyCase;
|
||||
$this->propertyCase = (int) $propertyCase;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace Hyperf\Database\Model\Concerns;
|
||||
|
||||
|
||||
use Hyperf\Utils\Str;
|
||||
|
||||
trait CamelCase
|
||||
{
|
||||
protected function keyTransform($key)
|
||||
{
|
||||
return Str::camel($key);
|
||||
}
|
||||
|
||||
public function getAttribute($key)
|
||||
{
|
||||
return parent::getAttribute($key) ?? parent::getAttribute(Str::snake($key));
|
||||
@ -23,19 +26,6 @@ trait CamelCase
|
||||
return parent::setAttribute(Str::snake($key), $value);
|
||||
}
|
||||
|
||||
protected function addMutatedAttributesToArray(array $attributes, array $mutatedAttributes)
|
||||
{
|
||||
foreach ($mutatedAttributes as $key) {
|
||||
if (!array_key_exists($this->keyTransform($key), $attributes)) {
|
||||
continue;
|
||||
}
|
||||
$attributes[$this->keyTransform($key)] = $this->mutateAttributeForArray(
|
||||
$this->keyTransform($key), $attributes[$this->keyTransform($key)]
|
||||
);
|
||||
}
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
public function jsonSerialize()
|
||||
{
|
||||
$array = [];
|
||||
@ -58,4 +48,23 @@ trait CamelCase
|
||||
{
|
||||
return parent::toArray();
|
||||
}
|
||||
}
|
||||
|
||||
protected function keyTransform($key)
|
||||
{
|
||||
return Str::camel($key);
|
||||
}
|
||||
|
||||
protected function addMutatedAttributesToArray(array $attributes, array $mutatedAttributes)
|
||||
{
|
||||
foreach ($mutatedAttributes as $key) {
|
||||
if (! array_key_exists($this->keyTransform($key), $attributes)) {
|
||||
continue;
|
||||
}
|
||||
$attributes[$this->keyTransform($key)] = $this->mutateAttributeForArray(
|
||||
$this->keyTransform($key),
|
||||
$attributes[$this->keyTransform($key)]
|
||||
);
|
||||
}
|
||||
return $attributes;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user