Skip to content

Commit

Permalink
修复window系统下CURD时因为目录分隔符不同而产生的异常 (#9)
Browse files Browse the repository at this point in the history
* 修复window系统下curd产生的异常

* 优化控制器模型绑定

Co-authored-by: chung <chung@99php.cn>
  • Loading branch information
zhongshaofa and 99php committed Jun 23, 2020
1 parent 7c072d5 commit 1753391
Showing 1 changed file with 53 additions and 32 deletions.
85 changes: 53 additions & 32 deletions vendor/zhongshaofa/easy-admin/src/curd/BuildCurd.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ public function setRelation($relationTable, $foreignKey, $primaryKey = null, $mo
if (!isset($this->tableColumns[$foreignKey])) {
throw new TableException("主表不存在外键字段:{$foreignKey}");
}
if (!empty($modelFilename)) {
$modelFilename = str_replace('/', $this->DS, $modelFilename);
}
try {
$colums = Db::query("SHOW FULL COLUMNS FROM {$this->tablePrefix}{$relationTable}");
$formatColums = [];
Expand Down Expand Up @@ -350,7 +353,7 @@ public function setRelation($relationTable, $foreignKey, $primaryKey = null, $mo
}

$modelFilename = empty($modelFilename) ? ucfirst(CommonTool::lineToHump($relationTable)) : $modelFilename;
$modelArray = explode('/', $modelFilename);
$modelArray = explode($this->DS, $modelFilename);
$modelName = array_pop($modelArray);

$relation = [
Expand All @@ -362,8 +365,8 @@ public function setRelation($relationTable, $foreignKey, $primaryKey = null, $mo
'delete' => $delete,
'tableColumns' => $formatColums,
];
if(!empty($bindSelectField)){
$relationArray = explode('\\',$modelFilename);
if (!empty($bindSelectField)) {
$relationArray = explode('\\', $modelFilename);
$this->tableColumns[$foreignKey]['bindSelectField'] = $bindSelectField;
$this->tableColumns[$foreignKey]['bindRelation'] = end($relationArray);
}
Expand All @@ -382,7 +385,7 @@ public function setRelation($relationTable, $foreignKey, $primaryKey = null, $mo
*/
public function setControllerFilename($controllerFilename)
{
$this->controllerFilename = $controllerFilename;
$this->controllerFilename = str_replace('/', $this->DS, $controllerFilename);
$this->buildViewJsUrl();
return $this;
}
Expand All @@ -394,7 +397,8 @@ public function setControllerFilename($controllerFilename)
*/
public function setModelFilename($modelFilename)
{
$this->modelFilename = $modelFilename;
$this->modelFilename = str_replace('/', $this->DS, $modelFilename);
$this->buildViewJsUrl();
return $this;
}

Expand Down Expand Up @@ -438,7 +442,7 @@ public function setForce($force)
*/
public function setCheckboxFieldSuffix($array)
{
$this->checkboxFieldSuffix = array_merge($this->checkboxFieldSuffix,$array);
$this->checkboxFieldSuffix = array_merge($this->checkboxFieldSuffix, $array);
return $this;
}

Expand All @@ -449,7 +453,7 @@ public function setCheckboxFieldSuffix($array)
*/
public function setRadioFieldSuffix($array)
{
$this->radioFieldSuffix = array_merge($this->radioFieldSuffix,$array);
$this->radioFieldSuffix = array_merge($this->radioFieldSuffix, $array);
return $this;
}

Expand All @@ -460,7 +464,7 @@ public function setRadioFieldSuffix($array)
*/
public function setImageFieldSuffix($array)
{
$this->imageFieldSuffix = array_merge($this->imageFieldSuffix,$array);
$this->imageFieldSuffix = array_merge($this->imageFieldSuffix, $array);
return $this;
}

Expand All @@ -471,7 +475,7 @@ public function setImageFieldSuffix($array)
*/
public function setImagesFieldSuffix($array)
{
$this->imagesFieldSuffix = array_merge($this->imagesFieldSuffix,$array);
$this->imagesFieldSuffix = array_merge($this->imagesFieldSuffix, $array);
return $this;
}

Expand All @@ -482,7 +486,7 @@ public function setImagesFieldSuffix($array)
*/
public function setFileFieldSuffix($array)
{
$this->fileFieldSuffix = array_merge($this->fileFieldSuffix,$array);
$this->fileFieldSuffix = array_merge($this->fileFieldSuffix, $array);
return $this;
}

Expand All @@ -493,7 +497,7 @@ public function setFileFieldSuffix($array)
*/
public function setFilesFieldSuffix($array)
{
$this->filesFieldSuffix = array_merge($this->filesFieldSuffix,$array);
$this->filesFieldSuffix = array_merge($this->filesFieldSuffix, $array);
return $this;
}

Expand All @@ -504,7 +508,7 @@ public function setFilesFieldSuffix($array)
*/
public function setDateFieldSuffix($array)
{
$this->dateFieldSuffix = array_merge($this->dateFieldSuffix,$array);
$this->dateFieldSuffix = array_merge($this->dateFieldSuffix, $array);
return $this;
}

Expand All @@ -515,7 +519,7 @@ public function setDateFieldSuffix($array)
*/
public function setSwitchFields($array)
{
$this->switchFields = array_merge($this->switchFields,$array);
$this->switchFields = array_merge($this->switchFields, $array);
return $this;
}

Expand All @@ -526,7 +530,7 @@ public function setSwitchFields($array)
*/
public function setSelectFileds($array)
{
$this->selectFileds = array_merge($this->selectFileds,$array);
$this->selectFileds = array_merge($this->selectFileds, $array);
return $this;
}

Expand All @@ -537,7 +541,7 @@ public function setSelectFileds($array)
*/
public function setSortFields($array)
{
$this->sortFields = array_merge($this->sortFields,$array);
$this->sortFields = array_merge($this->sortFields, $array);
return $this;
}

Expand All @@ -548,7 +552,7 @@ public function setSortFields($array)
*/
public function setIgnoreFields($array)
{
$this->ignoreFields = array_merge($this->ignoreFields,$array);
$this->ignoreFields = array_merge($this->ignoreFields, $array);
return $this;
}

Expand All @@ -567,7 +571,7 @@ public function getFileList()
*/
protected function buildViewJsUrl()
{
$nodeArray = explode('/', $this->controllerFilename);
$nodeArray = explode($this->DS, $this->controllerFilename);
$formatArray = [];
foreach ($nodeArray as $vo) {
$formatArray[] = CommonTool::humpToLine(lcfirst($vo));
Expand All @@ -583,7 +587,7 @@ protected function buildViewJsUrl()
$this->controllerNamespace = empty($namespaceSuffix) ? "app\admin\controller" : "app\admin\controller\\{$namespaceSuffix}";

// 主表模型命名
$modelArray = explode('/', $this->modelFilename);
$modelArray = explode($this->DS, $this->modelFilename);

$this->modelName = array_pop($modelArray);

Expand Down Expand Up @@ -984,9 +988,9 @@ protected function renderController()
]);
}
$selectList = '';
foreach ($this->relationArray as $relation){
if(!empty($relation['bindSelectField'])){
$relationArray = explode('\\',$relation['modelFilename']);
foreach ($this->relationArray as $relation) {
if (!empty($relation['bindSelectField'])) {
$relationArray = explode('\\', $relation['modelFilename']);
$selectList .= $this->buildSelectController(end($relationArray));
}
}
Expand All @@ -995,13 +999,16 @@ protected function renderController()
$selectList .= $this->buildSelectController($field);
}
}

$modelFilenameExtend = str_replace($this->DS,'\\',$this->modelFilename);

$controllerValue = CommonTool::replaceTemplate(
$this->getTemplate("controller{$this->DS}controller"),
[
'controllerName' => $this->controllerName,
'controllerNamespace' => $this->controllerNamespace,
'controllerAnnotation' => $this->tableComment,
'modelFilename' => "\app\admin\model\\{$this->modelFilename}",
'modelFilename' => "\app\admin\model\\{$modelFilenameExtend}",
'indexMethod' => $controllerIndexMethod,
'selectList' => $selectList,
]);
Expand Down Expand Up @@ -1036,8 +1043,8 @@ protected function renderModel()
}

$selectList = '';
foreach ($this->relationArray as $relation){
if(!empty($relation['bindSelectField'])){
foreach ($this->relationArray as $relation) {
if (!empty($relation['bindSelectField'])) {
$selectList .= $this->buildRelationSelectModel($relation['modelFilename'], $relation['bindSelectField']);
}
}
Expand All @@ -1047,11 +1054,18 @@ protected function renderModel()
}
}

$extendNamespaceArray = explode($this->DS, $this->modelFilename);
$extendNamespace = null;
if (count($extendNamespaceArray) > 1) {
array_pop($extendNamespaceArray);
$extendNamespace = '\\' . implode('\\', $extendNamespaceArray);
}

$modelValue = CommonTool::replaceTemplate(
$this->getTemplate("model{$this->DS}model"),
[
'modelName' => $this->modelName,
'modelNamespace' => "app\admin\model",
'modelNamespace' => "app\admin\model{$extendNamespace}",
'table' => $this->table,
'deleteTime' => $this->delete ? '"delete_time"' : 'false',
'relationList' => $relationList,
Expand All @@ -1072,11 +1086,18 @@ protected function renderModel()
}
}

$extendNamespaceArray = explode($this->DS, $val['modelFilename']);
$extendNamespace = null;
if (count($extendNamespaceArray) > 1) {
array_pop($extendNamespaceArray);
$extendNamespace = '\\' . implode('\\', $extendNamespaceArray);
}

$relationModelValue = CommonTool::replaceTemplate(
$this->getTemplate("model{$this->DS}model"),
[
'modelName' => $val['modelName'],
'modelNamespace' => "app\admin\model",
'modelNamespace' => "app\admin\model{$extendNamespace}",
'table' => $key,
'deleteTime' => $val['delete'] ? '"delete_time"' : 'false',
'relationList' => '',
Expand Down Expand Up @@ -1131,13 +1152,13 @@ protected function renderView()
$templateFile = "view{$this->DS}module{$this->DS}date";
if (isset($val['define']) && !empty($val['define'])) {
$define = $val['define'];
}else{
$define ='datetime';
} else {
$define = 'datetime';
}
if (!in_array($define, ['year', 'month', 'date', 'time', 'datetime'])) {
$define = 'datetime';
}
} elseif ($val['formType'] == 'radio') {
} elseif ($val['formType'] == 'radio') {
$templateFile = "view{$this->DS}module{$this->DS}radio";
if (isset($val['define']) && !empty($val['define'])) {
$define = $this->buildRadioView($field, '{in name="k" value="' . $val['default'] . '"}checked=""{/in}');
Expand Down Expand Up @@ -1206,13 +1227,13 @@ protected function renderView()
$templateFile = "view{$this->DS}module{$this->DS}date";
if (isset($val['define']) && !empty($val['define'])) {
$define = $val['define'];
}else{
$define ='datetime';
} else {
$define = 'datetime';
}
if (!in_array($define, ['year', 'month', 'date', 'time', 'datetime'])) {
$define = 'datetime';
}
} elseif ($val['formType'] == 'radio') {
} elseif ($val['formType'] == 'radio') {
$templateFile = "view{$this->DS}module{$this->DS}radio";
if (isset($val['define']) && !empty($val['define'])) {
$define = $this->buildRadioView($field, '{in name="k" value="$row.' . $field . '"}checked=""{/in}');
Expand Down

0 comments on commit 1753391

Please sign in to comment.