Merge pull request #482 from limingxinleo/1.0-db

Re-generate the fillable argument of Model when use refresh-fillable option, at the same time, the command will keep the fillable argument as default behaviours.
This commit is contained in:
李铭昕 2019-08-29 15:29:05 +08:00 committed by GitHub
commit 4ec6617ab1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 12 deletions

View File

@ -1,5 +1,9 @@
# v1.0.14 - TBD
## Changed
- [#482](https://github.com/hyperf-cloud/hyperf/pull/482) Re-generate the `fillable` argument of Model when use `refresh-fillable` option, at the same time, the command will keep the `fillable` argument as default behaviours.
## Fixed
- [#479](https://github.com/hyperf-cloud/hyperf/pull/479) Fixed typehint error when host of Elasticsearch client does not reached.

View File

@ -12,24 +12,34 @@ declare(strict_types=1);
namespace Hyperf\Database\Commands\Ast;
use Hyperf\Database\Commands\ModelOption;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
class ModelUpdateVisitor extends NodeVisitorAbstract
{
/**
* @var array
*/
protected $columns = [];
public function __construct($columns = [])
/**
* @var ModelOption
*/
protected $option;
public function __construct($columns = [], ModelOption $option)
{
$this->columns = $columns;
$this->option = $option;
}
public function leaveNode(Node $node)
{
switch ($node) {
case $node instanceof Node\Stmt\PropertyProperty:
if ($node->name == 'fillable') {
if ($node->name == 'fillable' && $this->option->isRefreshFillable()) {
$node = $this->rewriteFillable($node);
} elseif ($node->name == 'casts') {
$node = $this->rewriteCasts($node);

View File

@ -93,7 +93,8 @@ class ModelCommand extends Command
->setPrefix($this->getOption('prefix', 'prefix', $pool, ''))
->setInheritance($this->getOption('inheritance', 'commands.db:model.inheritance', $pool, 'Model'))
->setUses($this->getOption('uses', 'commands.db:model.uses', $pool, 'Hyperf\DbConnection\Model\Model'))
->setForceCasts($this->getOption('force-casts', 'commands.db:model.force_casts', $pool, false));
->setForceCasts($this->getOption('force-casts', 'commands.db:model.force_casts', $pool, false))
->setRefreshFillable($this->getOption('refresh-fillable', 'commands.db:model.refresh_fillable', $pool, false));
if ($table) {
$this->createModel($table, $option);
@ -112,6 +113,7 @@ class ModelCommand extends Command
$this->addOption('prefix', 'P', InputOption::VALUE_OPTIONAL, 'What prefix that you want the Model set.');
$this->addOption('inheritance', 'i', InputOption::VALUE_OPTIONAL, 'The inheritance that you want the Model extends.');
$this->addOption('uses', 'U', InputOption::VALUE_OPTIONAL, 'The default class uses of the Model.');
$this->addOption('refresh-fillable', null, InputOption::VALUE_NONE, 'Whether generate fillable argement for model.');
}
protected function getSchemaBuilder(string $poolName): MySqlBuilder
@ -158,7 +160,10 @@ class ModelCommand extends Command
$stms = $this->astParser->parse(file_get_contents($path));
$traverser = new NodeTraverser();
$visitor = make(ModelUpdateVisitor::class, ['columns' => $columns]);
$visitor = make(ModelUpdateVisitor::class, [
'columns' => $columns,
'option' => $option,
]);
$traverser->addVisitor($visitor);
$stms = $traverser->traverse($stms);
$code = $this->printer->prettyPrintFile($stms);
@ -203,7 +208,7 @@ class ModelCommand extends Command
protected function getOption(string $name, string $key, string $pool = 'default', $default = null)
{
$result = $this->input->getOption($name);
$nonInput = $name === 'force-casts' ? false : null;
$nonInput = in_array($name, ['force-casts', 'refresh-fillable']) ? false : null;
if ($result === $nonInput) {
$result = $this->config->get("databases.{$pool}.{$key}", $default);
}

View File

@ -44,6 +44,11 @@ class ModelOption
*/
protected $uses;
/**
* @var bool
*/
protected $refreshFillable;
public function getPool(): string
{
return $this->pool;
@ -99,21 +104,25 @@ class ModelOption
return $this;
}
/**
* @return string
*/
public function getUses(): string
{
return $this->uses;
}
/**
* @param string $uses
* @return ModelOption
*/
public function setUses(string $uses): ModelOption
{
$this->uses = $uses;
return $this;
}
public function isRefreshFillable(): bool
{
return $this->refreshFillable;
}
public function setRefreshFillable(bool $refreshFillable): ModelOption
{
$this->refreshFillable = $refreshFillable;
return $this;
}
}