fangfubing 2 săptămâni în urmă
părinte
comite
5ba1807691
7 a modificat fișierele cu 254 adăugiri și 7 ștergeri
  1. 1 1
      .gitignore
  2. 48 0
      TPL/get.tpl
  3. 81 0
      TPL/query.tpl
  4. 49 0
      TPL/set.tpl
  5. 54 0
      TPL/setGet.tpl
  6. 8 0
      inter/mysqlStruct/Table.php
  7. 13 6
      main.php

+ 1 - 1
.gitignore

@@ -1,5 +1,5 @@
 .vscode
 .idea
-.gen
+gen/
 
 test.php

+ 48 - 0
TPL/get.tpl

@@ -0,0 +1,48 @@
+{assign var="datetime" value=$file->GetFileCreateTime()}
+{assign var="namespace" value=$file->GetNamespace()}
+{assign var="classes" value=$file->GetClasses()}
+{assign var="uses" value=$file->GetUses()}
+<?php
+/**
+ * Created by FFB.
+ * Company: Shenzhen Zhixun Information Tec. Nanchang Com.
+ * Copyright: ShenzhenZhixun
+ * Email: eastbus@126.com
+ * User: FFB
+ * Date: {$datetime}
+ * 文件不允许修改,自动生成
+ */
+
+namespace {$namespace};
+
+{section name=index loop=$uses}
+use {{$uses[index]}};
+{/section}
+{section name=index loop=$classes}
+{assign var="className" value=$classes[index]->GetClassName()}
+{assign var="tableName" value=$classes[index]->GetTable()->GetName()}
+{assign var="attrs" value=$classes[index]->GetAttrs()}
+{assign var="extends" value=$classes[index]->GetExtends()}
+class {$className}{foreach item=e from=$extends key=i}{if $i == 0} extends {/if}{$e}{/foreach}{
+    public static function tableName()
+    {
+        return '{literal}{{{/literal}{{$tableName}}{literal}}}{/literal}';
+    }
+    {section name=index loop=$attrs}
+    {assign var="fieldName" value=$attrs[index]->GetColumn()->GetName()}
+    {assign var="fieldComment" value=$attrs[index]->GetColumn()->GetComment()}
+    {assign var="attrName" value=$attrs[index]->GetName()}
+
+    /**
+     * ------------ 字段 {{$fieldName}} 
+     * ------------ 表字段注释: {{$fieldComment}}
+     */
+    public function get{{$attrName}}()
+    {
+        return $this->{{$fieldName}} ?? null;
+    }
+    // ---------------------------------------------
+    {/section}
+
+}
+{/section}

+ 81 - 0
TPL/query.tpl

@@ -0,0 +1,81 @@
+{assign var="datetime" value=$file->GetFileCreateTime()}
+{assign var="namespace" value=$file->GetNamespace()}
+{assign var="classes" value=$file->GetClasses()}
+{assign var="uses" value=$file->GetUses()}
+<?php
+/**
+ * Created by FFB.
+ * Company: Shenzhen Zhixun Information Tec. Nanchang Com.
+ * Copyright: ShenzhenZhixun
+ * Email: eastbus@126.com
+ * User: FFB
+ * Date: {$datetime}
+ * 文件不允许修改,自动生成
+ */
+
+namespace {$namespace};
+
+{section name=index loop=$uses}
+use {{$uses[index]}};
+{/section}
+{section name=index loop=$classes}
+{assign var="className" value=$classes[index]->GetClassName()}
+{assign var="tableName" value=$classes[index]->GetTable()->GetName()}
+{assign var="tableNameHump" value=$classes[index]->GetTable()->GetNameForHump()}
+{assign var="attrs" value=$classes[index]->GetAttrs()}
+{assign var="extends" value=$classes[index]->GetExtends()}
+class {$className}{foreach item=e from=$extends key=i}{if $i == 0} extends {/if}{$e}{/foreach}{
+    public function __construct($config = [])
+    {
+        parent::__construct({$tableNameHump}::class, $config);
+    }
+
+    /**
+     * @param null $db
+     * @return {$tableNameHump} | null | array
+     */
+    public function one($db = null)
+    {
+        return parent::one($db);
+    }
+
+    /**
+     * @param null $db
+     * @return array | {$tableNameHump}[]
+     */
+    public function all($db = null)
+    {
+        return parent::all($db);
+    }
+
+    public static function tableName()
+    {
+        return '{literal}{{{/literal}{{$tableName}}{literal}}}{/literal}';
+    }
+    {section name=index loop=$attrs}
+    {assign var="fieldName" value=$attrs[index]->GetColumn()->GetName()}
+    {assign var="fieldComment" value=$attrs[index]->GetColumn()->GetComment()}
+    {assign var="attrName" value=$attrs[index]->GetName()}
+
+    /**
+     * ------------ 字段 {{$fieldName}} 
+     * ------------ 表字段注释: {{$fieldComment}}
+     * 如果$val不是function,通过传入的值,直接组成['tableName' => $val]给到where方法
+     * 如果是function,则闭包函数的类型为  function(string $tableName, ActiveQuery $activeQuery)
+     * @param mixed | callable 
+     */
+    public function filterBy{{$attrName}}($val): self
+    {
+        if(is_callable($val)){
+            $val("{{$fieldName}}", $this);
+            return $this;
+        }
+
+        $this->andWhere(["{{$fieldName}}" => $val]);
+        return $this;
+    }
+    // ---------------------------------------------
+    {/section}
+
+}
+{/section}

+ 49 - 0
TPL/set.tpl

@@ -0,0 +1,49 @@
+{assign var="datetime" value=$file->GetFileCreateTime()}
+{assign var="namespace" value=$file->GetNamespace()}
+{assign var="classes" value=$file->GetClasses()}
+{assign var="uses" value=$file->GetUses()}
+<?php
+/**
+ * Created by FFB.
+ * Company: Shenzhen Zhixun Information Tec. Nanchang Com.
+ * Copyright: ShenzhenZhixun
+ * Email: eastbus@126.com
+ * User: FFB
+ * Date: {$datetime}
+ * 文件不允许修改,自动生成
+ */
+
+namespace {$namespace};
+
+{section name=index loop=$uses}
+use {{$uses[index]}};
+{/section}
+{section name=index loop=$classes}
+{assign var="className" value=$classes[index]->GetClassName()}
+{assign var="tableName" value=$classes[index]->GetTable()->GetName()}
+{assign var="attrs" value=$classes[index]->GetAttrs()}
+{assign var="extends" value=$classes[index]->GetExtends()}
+class {$className}{foreach item=e from=$extends key=i}{if $i == 0} extends {/if}{$e}{/foreach}{
+    public static function tableName()
+    {
+        return '{literal}{{{/literal}{{$tableName}}{literal}}}{/literal}';
+    }
+    {section name=index loop=$attrs}
+    {assign var="fieldName" value=$attrs[index]->GetColumn()->GetName()}
+    {assign var="fieldComment" value=$attrs[index]->GetColumn()->GetComment()}
+    {assign var="attrName" value=$attrs[index]->GetName()}
+
+    /**
+     * ------------ 字段 {{$fieldName}} 
+     * ------------ 表字段注释: {{$fieldComment}}
+     */
+    public function set{{$attrName}}($val): self
+    {
+        $this->setAttr("{{$fieldName}}", $val);
+        return $this;
+    }
+    // ---------------------------------------------
+    {/section}
+
+}
+{/section}

+ 54 - 0
TPL/setGet.tpl

@@ -0,0 +1,54 @@
+{assign var="datetime" value=$file->GetFileCreateTime()}
+{assign var="namespace" value=$file->GetNamespace()}
+{assign var="classes" value=$file->GetClasses()}
+{assign var="uses" value=$file->GetUses()}
+<?php
+/**
+ * Created by FFB.
+ * Company: Shenzhen Zhixun Information Tec. Nanchang Com.
+ * Copyright: ShenzhenZhixun
+ * Email: eastbus@126.com
+ * User: FFB
+ * Date: {$datetime}
+ * 文件不允许修改,自动生成
+ */
+
+namespace {$namespace};
+
+{section name=index loop=$uses}
+use {{$uses[index]}};
+{/section}
+{section name=index loop=$classes}
+{assign var="className" value=$classes[index]->GetClassName()}
+{assign var="tableName" value=$classes[index]->GetTable()->GetName()}
+{assign var="attrs" value=$classes[index]->GetAttrs()}
+{assign var="extends" value=$classes[index]->GetExtends()}
+class {$className}{foreach item=e from=$extends key=i}{if $i == 0} extends {/if}{$e}{/foreach}{
+    public static function tableName()
+    {
+        return '{literal}{{{/literal}{{$tableName}}{literal}}}{/literal}';
+    }
+    {section name=index loop=$attrs}
+    {assign var="fieldName" value=$attrs[index]->GetColumn()->GetName()}
+    {assign var="fieldComment" value=$attrs[index]->GetColumn()->GetComment()}
+    {assign var="attrName" value=$attrs[index]->GetName()}
+
+    /**
+     * ------------ 字段 {{$fieldName}} 
+     * ------------ 表字段注释: {{$fieldComment}}
+     */
+    public function set{{$attrName}}($val): self
+    {
+        $this->setAttr("{{$fieldName}}", $val);
+        return $this;
+    }
+
+    public function get{{$attrName}}()
+    {
+        return $this->{{$fieldName}} ?? null;
+    }
+    // ---------------------------------------------
+    {/section}
+
+}
+{/section}

+ 8 - 0
inter/mysqlStruct/Table.php

@@ -2,6 +2,8 @@
 
 namespace inter\mysqlStruct;
 
+use inter\utils\Names;
+
 class Table
 {
     private $name;
@@ -25,6 +27,12 @@ class Table
         return $this->name;
     }
 
+    // 驼峰
+    public function GetNameForHump(): string
+    {
+        return ucfirst(Names::Camelize($this->name));
+    }
+
     /**
      * @return Column[]
      */

+ 13 - 6
main.php

@@ -15,17 +15,23 @@ $database = 'new_energy';
 $dbConfig = new Config('101.37.90.21', "", 'root', 'QAZwsx!@#456', $database);
 $db = new DB($dbConfig);
 $dbProcess = new Struct($db);
-$dbProcess->SelectDatabase($database)->AddUseTable("jdz_city_exchange_shop_area");
+$dbProcess->SelectDatabase($database);//->AddUseTable("dh_admin_commission_account");
 $dbStruct = $dbProcess->GetDatabase();
 
 $config = new ProcessConfig();
-//$config->fileNamePrefix = "T";
+
 $config->savePath = "gen";
-$config->SetNamespace('common/table/'.$database);
 $config->isHasGetFunc = true;
 $config->isHasSetFunc = true;
-$config->addUse("common/table/TableModelBase");
-$config->extends[] = "TableModelBase";
+//$config->fileNamePrefix = "";
+//$config->SetNamespace('common/table/'.$database);
+//$config->addUse("common/table/TableModelBase");
+//$config->extends[] = "TableModelBase";
+
+$config->fileNameSuffix = "Query";
+$config->addUse("yii/db/ActiveQuery");
+$config->SetNamespace('common/table/'.$database.'/query');
+$config->extends[] = "ActiveQuery";
 $opt = new DbStruct2ClassProcess($config, $dbStruct);
 $files = $opt->GetFiles();
 
@@ -41,7 +47,8 @@ foreach ($files as $file) {
     $smarty->assign('file', $file);
 
     // 渲染模板
-    $content = $smarty->fetch('template.tpl');
+    //$content = $smarty->fetch('template.tpl');
+    $content = $smarty->fetch('query.tpl');
 
     // 保存内容到文件
     $filePath = $file->GetPath() . '/' . $file->GetFileName();