Skip to content

Commit

Permalink
调整模型生成逻辑,生成表结构定义在基类,实际的模型类继承基类
Browse files Browse the repository at this point in the history
修改了表结构重新生成模型,也不用担心会覆盖掉模型类中自己写的方法
  • Loading branch information
Yurunsoft committed Feb 18, 2019
1 parent 5e2485d commit 0c7d962
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 59 deletions.
44 changes: 30 additions & 14 deletions src/Tool/Tools/Generate/Model/ModelGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,21 @@ class ModelGenerate
* @Arg(name="prefix", type=ArgType::STRING, default="", comments="传值则去除该表前缀")
* @Arg(name="include", type=ArgType::ARRAY, default={}, comments="要包含的表名,以半角逗号分隔")
* @Arg(name="exclude", type=ArgType::ARRAY, default={}, comments="要排除的表名,以半角逗号分隔")
* @Arg(name="override", type=ArgType::BOOLEAN, default=false, comments="是否覆盖已存在的文件,请慎重!(true/false)")
* @Arg(name="override", type=ArgType::STRING, default=false, comments="是否覆盖已存在的文件,请慎重!true-全覆盖;false-不覆盖;base-覆盖基类;model-覆盖模型类;默认缺省状态为false")
* @return void
*/
public function generate($namespace, $database, $poolName, $prefix, $include, $exclude, $override)
{
$override = (string)$override;
switch($override)
{
case 'base':
break;
case 'model':
break;
default:
$override = (bool)json_decode($override);
}
$query = Db::query($poolName);
// 数据库
if(null === $database)
Expand All @@ -52,6 +62,8 @@ public function generate($namespace, $database, $poolName, $prefix, $include, $e
// model保存路径
$modelPath = Imi::getNamespacePath($namespace);
File::createDir($modelPath);
$baseModelPath = $modelPath . '/Base';
File::createDir($baseModelPath);
foreach($list as $item)
{
$table = $item['TABLE_NAME'];
Expand All @@ -64,21 +76,13 @@ public function generate($namespace, $database, $poolName, $prefix, $include, $e
$fileName = File::path($modelPath, $className . '.php');
if(is_file($fileName))
{
if($override)
{
echo 'Override ', $table, '...', PHP_EOL;
}
else
if(false === $override)
{
// 不覆盖
echo 'Skip ', $table, '...', PHP_EOL;
continue;
}
}
else
{
echo 'Generating ', $table, '...', PHP_EOL;
}
$data = [
'namespace' => $namespace,
'className' => $className,
Expand All @@ -90,8 +94,20 @@ public function generate($namespace, $database, $poolName, $prefix, $include, $e
];
$fields = $query->bindValue(':table', $table)->execute('show full columns from ' . $table)->getArray();
$this->parseFields($fields, $data, 'VIEW' === $item['TABLE_TYPE']);
$content = $this->renderTemplate($data);
File::writeFile($fileName, $content);

if(true === $override || 'base' === $override)
{
echo 'Generating ', $table, ' BaseClass...', PHP_EOL;
$baseContent = $this->renderTemplate('base-template', $data);
file_put_contents(File::path($baseModelPath, $className . 'Base.php'), $baseContent);
}

if(true === $override || 'model' === $override)
{
echo 'Generating ', $table, ' Class...', PHP_EOL;
$content = $this->renderTemplate('template', $data);
file_put_contents($fileName, $content);
}
}
echo 'Complete', PHP_EOL;
}
Expand Down Expand Up @@ -209,11 +225,11 @@ public function parseFieldType($text, &$typeName, &$length, &$accuracy)
* @param string $data
* @return string
*/
private function renderTemplate($data)
private function renderTemplate($template, $data)
{
extract($data);
ob_start();
include __DIR__ . '/template.tpl';
include __DIR__ . '/' . $template . '.tpl';
return ob_get_clean();
}

Expand Down
58 changes: 58 additions & 0 deletions src/Tool/Tools/Generate/Model/base-template.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?= '<?php' ?>

namespace <?= $namespace ?>\Base;

use Imi\Model\Model;
use Imi\Model\Annotation\Column;

/**
* <?= $className ?>Base

<?php foreach($fields as $field):?>
* @property <?= $field['phpType'] ?> $<?= $field['varName'] ?>

<?php endforeach;?>
*/
abstract class <?= $className ?>Base extends Model
{
<?php
foreach($fields as $field):
?>
/**
* <?= $field['name'] ?><?= '' === $field['comment'] ? '' : (' - ' . $field['comment']) ?>
* @Column(name="<?= $field['name'] ?>", type="<?= $field['type'] ?>", length=<?= $field['length'] ?>, accuracy=<?= $field['accuracy'] ?>, nullable=<?= json_encode($field['nullable']) ?>, default="<?= $field['default'] ?>", isPrimaryKey=<?= json_encode($field['isPrimaryKey']) ?>, primaryKeyIndex=<?= $field['primaryKeyIndex'] ?>, isAutoIncrement=<?= json_encode($field['isAutoIncrement']) ?>)
* @var <?= $field['phpType'] ?>
*/
protected $<?= $field['varName'] ?>;
/**
* 获取 <?= $field['varName'] ?><?= '' === $field['comment'] ? '' : (' - ' . $field['comment']) ?>
*
* @return <?= $field['phpType'] ?>
*/
public function get<?= ucfirst($field['varName']) ?>()
{
return $this-><?= $field['varName'] ?>;
}

/**
* 赋值 <?= $field['varName'] ?><?= '' === $field['comment'] ? '' : (' - ' . $field['comment']) ?>

* @param <?= $field['phpType'] ?> $<?= $field['varName'] ?> <?= $field['name'] ?>

* @return static
*/
public function set<?= ucfirst($field['varName']) ?>($<?= $field['varName'] ?>)
{
$this-><?= $field['varName'] ?> = $<?= $field['varName'] ?>;
return $this;
}

<?php
endforeach;
?>
}
48 changes: 3 additions & 45 deletions src/Tool/Tools/Generate/Model/template.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,15 @@ use Imi\Model\Model;
use Imi\Model\Annotation\Table;
use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\Entity;
use <?= $namespace ?>\Base\<?= $className ?>Base;

/**
* <?= $className ?>

* @Entity
* @Table(name="<?= $table['name'] ?>"<?php if(isset($table['id'][0])):?>, id={<?= '"', implode('", "', $table['id']), '"' ?>}<?php endif;?>)
<?php foreach($fields as $field):?>
* @property <?= $field['phpType'] ?> $<?= $field['varName'] ?>

<?php endforeach;?>
*/
class <?= $className ?> extends Model
class <?= $className ?> extends <?= $className ?>Base
{
<?php
foreach($fields as $field):
?>
/**
* <?= $field['name'] ?><?= '' === $field['comment'] ? '' : (' - ' . $field['comment']) ?>
* @Column(name="<?= $field['name'] ?>", type="<?= $field['type'] ?>", length=<?= $field['length'] ?>, accuracy=<?= $field['accuracy'] ?>, nullable=<?= json_encode($field['nullable']) ?>, default="<?= $field['default'] ?>", isPrimaryKey=<?= json_encode($field['isPrimaryKey']) ?>, primaryKeyIndex=<?= $field['primaryKeyIndex'] ?>, isAutoIncrement=<?= json_encode($field['isAutoIncrement']) ?>)
* @var <?= $field['phpType'] ?>
*/
protected $<?= $field['varName'] ?>;
/**
* 获取 <?= $field['varName'] ?><?= '' === $field['comment'] ? '' : (' - ' . $field['comment']) ?>
*
* @return <?= $field['phpType'] ?>
*/
public function get<?= ucfirst($field['varName']) ?>()
{
return $this-><?= $field['varName'] ?>;
}

/**
* 赋值 <?= $field['varName'] ?><?= '' === $field['comment'] ? '' : (' - ' . $field['comment']) ?>

* @param <?= $field['phpType'] ?> $<?= $field['varName'] ?> <?= $field['name'] ?>

* @return static
*/
public function set<?= ucfirst($field['varName']) ?>($<?= $field['varName'] ?>)
{
$this-><?= $field['varName'] ?> = $<?= $field['varName'] ?>;
return $this;
}
<?php
endforeach;
?>
}
}

0 comments on commit 0c7d962

Please sign in to comment.