【TP5】根据数据库字段注释使用同一模板进行增删查
author:咔咔
wechat:fangkangfk
有没有在添加跟修改的时候很烦,需要写很多页面,修改还需要追加数据,很烦,很烦
下来就解决这样的问题,所有模块的添加,修改,查看,都使用同一个模板
首先从数据库设计开始
注释解释:
域名地址:这个在添加或者修改时在lable标签里边写的
add=text:这个最终会以这个判断这个字段是什么类型,并且显示不同的标签
这里放一个写的比较全的
下来咱们先写一个获取数据库注释的方法
第一个方法是获取数据库注释,第二个方法是对注释进行业务需求的组装
-
/**
-
* author:咔咔
-
*
-
* 获取字段注释类型
-
* @param $table 表名
-
* @param bool $includePrimary
-
* @return array
-
*/
-
public function getFileCommon($table, $includePrimary = false)
-
{
-
$prefix = config('database.prefix');
-
$data = Db::query('SHOW FULL COLUMNS FROM '.$prefix.$table);
-
-
foreach($data as $row){
-
if ($row['Comment']) {
-
$row = array_merge($row, $this->_parse_comments($row['Comment']));
-
}else {
-
$row['DisplayName'] = $row['Field'];
-
}
-
-
$type = $row['Type'];
-
if (preg_match('/^varchar/i', $type)) {
-
$type = str_replace('varchar', '字符串', $type);
-
} else if (preg_match('/^int/i', $type)) {
-
$type = str_replace('int', '数字', $type);
-
} else if (preg_match('/^tinyint/i', $type)) {
-
$type = str_replace('tinyint', '数字', $type);
-
preg_match('/\((\d+)\)/i', $type, $matches);
-
$length = $matches[1];
-
} else if (preg_match('/^text/i', $type)) {
-
$type = str_replace('text', '文本', $type);
-
} else if (preg_match('/^datetime/i', $type)) {
-
$type = str_replace('datetime', '时间:YYY-MM-DD HH:II:SS', $type);
-
}
-
$row['DisplayType'] = $type;
-
-
if ($includePrimary) {
-
$fields[] = $row;
-
} else {
-
if ($row['Key'] != 'PRI') {
-
$fields[] = $row;
-
}
-
}
-
}
-
-
// 整理:以name为key
-
$new_fields = array();
-
foreach ($fields as $value) {
-
$name = $value['Field'];
-
$new_fields[$name] = $value;
-
}
-
return $new_fields;
-
}
-
-
-
/**
-
* author:咔咔
-
*
-
* 提取字段注释跟显示类型
-
* @param $comments 注释
-
* @return array
-
*/
-
protected function _parse_comments($comments)
-
{
-
$parts = explode("|", $comments);
-
-
$comment = $parts[0];
-
$params_string = $parts[1];
-
-
if ($params_string) {
-
parse_str($params_string, $params);
-
foreach ($params as $key => $value) {
-
if ($value == 'true') {
-
$value = true;
-
}
-
-
if ($value == 'false') {
-
$value = false;
-
}
-
-
$params[$key] = $value;
-
}
-
} else {
-
$params = array();
-
}
-
-
$result = array('DisplayName' => $comment, 'Params' => $params);
-
return $result;
-
}
怎么使用?
我的domain控制器继承了base控制器,上面俩个方法在base控制器
我们来看看数据
这个时候是不是纳闷了,怎么就一个数据,那个ID哪去了。我们可以看看第一个方法,下图我框出来的这块
别纳闷PRi是mysql约束主键的意思,所以只有一个数据
下来咱们准备写一下模板
最终添加模板就这样
我们还需要写公共模板,这里我就粘贴已经写好的一份
-
{include file="../../../application/admin/view/public/head" /}
-
-
<?php foreach ($fields as $info):?>
-
-
<?php
-
$type = $info['Params']['add'];
-
$validateClass = "validate[required]";
-
?>
-
-
<?php if ($info['Params']['add'] == 'ignore'): ?>
-
<?php continue; ?>
-
<?php endif; ?>
-
-
<?php if ($info['Params']['add'] == 'hidden'): ?>
-
<input name="<?php echo $info['Field'];?>" type="hidden" value="<?php echo htmlspecialchars($info['Value']); ?>" />
-
<?php continue; ?>
-
<?php endif; ?>
-
<tr>
-
-
<td>
-
<?php if ($type == 'text'): ?>
-
-
<div class="layui-form-item">
-
<label class="layui-form-label"><?php echo $info['DisplayName'];?></label>
-
<div class="layui-input-block">
-
<input id="id_<?php echo $info['Field'];?>" lay-verify="name" class="layui-input" name="<?php echo $info['Field'];?>">
-
</div>
-
</div>
-
-
<?php elseif ($type == 'bool'): ?>
-
-
<label class="radio">
-
<input type="radio" name="<?php echo $info['Field'];?>" class="<?php echo $validateClass; ?>" value="1" checked>
-
是
-
</label>
-
<label class="radio">
-
<input type="radio" name="<?php echo $info['Field'];?>" class="<?php echo $validateClass; ?>" value="2">
-
否
-
</label>
-
-
-
<?php elseif ($type == 'radio'): ?>
-
<div class="layui-form-item">
-
<label class="layui-form-label">类型</label>
-
<div class="layui-input-block">
-
-
<?php $items = explode(",", $info['Params']['items']); ?>
-
-
<?php foreach ($items as $value): ?>
-
<?php
-
$parts = explode('#', $value, 2);
-
$displayText = $parts[0];
-
if (count($parts) == 2) {
-
$displayValue = $parts[1];
-
} else {
-
$displayValue = $parts[0];
-
}
-
?>
-
-
<label class="radio">
-
<input type="radio" name="<?php echo $info['Field'];?>" value="<?php echo $displayValue; ?>" class="<?php echo $validateClass; ?>" checked> <?php echo $displayText; ?>
-
</label>
-
-
-
<?php endforeach; ?>
-
</div>
-
</div>
-
-
<?php elseif ($type == 'checkbox'): ?>
-
-
<?php $items = explode(",", $info['Params']['items']); ?>
-
-
<?php foreach ($items as $value): ?>
-
<?php
-
$parts = explode('#', $value, 2);
-
$displayText = $parts[0];
-
if (count($parts) == 2) {
-
$displayValue = $parts[1];
-
} else {
-
$displayValue = $parts[0];
-
}
-
-
?>
-
<label class="checkbox">
-
<input type="checkbox" name="<?php echo $info['Field'];?>[]" value="<?php echo $displayValue;?>" class="<?php echo $validateClass; ?>"> <?php echo $displayText;?>
-
</label>
-
<?php endforeach; ?>
-
-
<?php elseif ($type == 'textarea'): ?>
-
-
<textarea name="<?php echo $info['Field'];?>" rows="10" cols="100" class="input-xxlarge <?php echo $validateClass; ?>"><?php echo htmlspecialchars($info['Value']); ?></textarea>
-
-
<?php elseif ($type == 'textarea_html'): ?>
-
-
<textarea name="<?php echo $info['Field'];?>" rows="20" cols="100" class="input-xxlarge <?php echo $validateClass; ?>"><?php echo htmlspecialchars($info['Value']); ?></textarea>
-
-
<?php elseif ($type == 'label'): ?>
-
-
<?php echo htmlspecialchars($info['Default']);?>
-
-
<?php elseif ($type == 'datetime'): ?>
-
-
<?php continue; ?>
-
-
<?php elseif ($type == 'image_upload'): ?>
-
-
<input name="<?php echo $info['Field'];?>" type="file" class="<?php echo $validateClass; ?>" />
-
-
<?php elseif ($type == 'file_upload'): ?>
-
-
<input name="<?php echo $info['Field'];?>" type="file" class="<?php echo $validateClass; ?>" />
-
-
<?php else: ?>
-
-
<textarea name="<?php echo $info['Field'];?>" rows="10" cols="100" class="input-xxlarge <?php echo $validateClass; ?>"><?php echo htmlspecialchars($info['Value']); ?></textarea>
-
-
<?php endif; ?>
-
</td>
-
</tr>
-
<?php endforeach;?>
-
-
-
在公共模板的顶部我们将字段自定义类型定义了一个变量
我们的这个dn_info自定义类型是text,所以就会将这个input输出 ,不管修改还是添加都是需要name值的,所以name值也会是动态的,我们组装的数据里边也有
这个就是最终效果
上面这个是关于添加的,我们还需要做个修改的,修改是里边是有值得
跟添加的原理一样,只不过需要将字段值组装
看一下数据结构
在来一个修改的模板
-
{include file="../../../application/admin/view/public/head" /}
-
<?php foreach ($fields as $info):?>
-
-
<?php
-
$type = $info['Params']['edit'];
-
$validateClass = "validate[required]";
-
?>
-
-
<?php if ($info['Params']['edit'] == 'ignore'): ?>
-
<?php continue; ?>
-
<?php endif; ?>
-
-
<?php if ($info['Params']['edit'] == 'hidden'): ?>
-
<input name="<?php echo $info['Field'];?>" type="hidden" value="<?php echo htmlspecialchars($info['Value']); ?>" />
-
<?php continue; ?>
-
<?php endif; ?>
-
-
<tr>
-
<td>
-
<?php if ($type == 'text'): ?>
-
-
<div class="layui-form-item">
-
<label class="layui-form-label"><?php echo $info['DisplayName'];?></label>
-
<div class="layui-input-block">
-
<input id="id_<?php echo $info['Field'];?>" lay-verify="name" class="layui-input" name="<?php echo $info['Field'];?>" value="<?php echo $info['Value'] ?>">
-
</div>
-
</div>
-
-
<?php elseif ($type == 'bool'): ?>
-
-
<label class="radio">
-
<input type="radio" name="<?php echo $info['Field'];?>" value="1" class="<?php echo $validateClass; ?>" <?php if ($info[Value] == 1) echo 'checked'; ?>>
-
是
-
</label>
-
<label class="radio">
-
<input type="radio" name="<?php echo $info['Field'];?>" value="2" class="<?php echo $validateClass; ?>" <?php if ($info[Value] == 2) echo 'checked'; ?>>
-
否
-
</label>
-
-
<?php elseif ($type == 'radio'): ?>
-
-
<?php $items = explode(",", $info['Params']['items']); ?>
-
-
<?php foreach ($items as $value): ?>
-
<?php
-
$parts = explode('#', $value, 2);
-
$displayText = $parts[0];
-
if (count($parts) == 2) {
-
$displayValue = $parts[1];
-
} else {
-
$displayValue = $parts[0];
-
}
-
?>
-
<label class="radio">
-
<input type="radio" name="<?php echo $info['Field'];?>" value="<?php echo $displayValue; ?>" class="<?php echo $validateClass; ?>" <?php if ($displayValue == $info['Value']) echo 'checked'; ?>>
-
<?php echo $displayText; ?>
-
</label>
-
<?php endforeach; ?>
-
-
<?php elseif ($type == 'checkbox'): ?>
-
-
<?php
-
$items = explode(",", $info['Params']['items']);
-
$currentItems = explode(' ', $info['Value']);
-
?>
-
-
<?php foreach ($items as $value): ?>
-
<?php
-
$parts = explode('#', $value, 2);
-
$displayText = $parts[0];
-
if (count($parts) == 2) {
-
$displayValue = $parts[1];
-
} else {
-
$displayValue = $parts[0];
-
}
-
?>
-
<label class="checkbox">
-
<input type="checkbox" name="<?php echo $info['Field'];?>[]" value="<?php echo $displayValue;?>" class="<?php echo $validateClass; ?>" <?php if (in_array($value, $currentItems)) echo 'checked'; ?>> <?php echo $displayText;?>
-
</label>
-
<?php endforeach; ?>
-
-
<?php elseif ($type == 'textarea'): ?>
-
-
<textarea name="<?php echo $info['Field'];?>" rows="10" cols="100" class="input-xxlarge <?php echo $validateClass; ?>"><?php echo htmlspecialchars($info['Value']); ?></textarea>
-
-
<?php elseif ($type == 'textarea_markdown'): ?>
-
-
<textarea name="<?php echo $info['Field'];?>" rows="20" cols="100" data-provide="markdown" id='markdownArea' class="input-xxlarge md-input <?php echo $validateClass; ?>"><?php echo htmlspecialchars($info['Value']); ?></textarea>
-
-
<br /><a href='http://wowubuntu.com/markdown/' target='_blank'>Markdown 语法说明</a>
-
-
<?php elseif ($type == 'textarea_html'): ?>
-
-
<textarea name="<?php echo $info['Field'];?>" rows="20" cols="100" class="input-xxlarge <?php echo $validateClass; ?>"><?php echo htmlspecialchars($info['Value']); ?></textarea>
-
-
<?php elseif ($type == 'label'): ?>
-
-
<?php echo htmlspecialchars($info['Value']);?>
-
-
<?php elseif ($type == 'datetime'): ?>
-
-
<input name="<?php echo $info['Field'];?>" class="input-xxlarge <?php echo $validateClass; ?>" value="<?php echo $info['Value']; ?>" />
-
-
<?php elseif ($type == 'image_upload'): ?>
-
-
<img src="<?php echo '/image/view/' . $info['Params']['storage'] . '/' . $info['Value'] . '/small'; ?>" data-src="holder.js/200x200" alt="" style="max-height:200px;" />
-
<input name="<?php echo $info['Field'];?>" type="file" />
-
-
<?php elseif ($type == 'file_upload'): ?>
-
-
<input name="<?php echo $info['Field'];?>" type="file" />
-
-
<?php else: ?>
-
-
<textarea name="<?php echo $info['Field'];?>" rows="10" cols="100" class="input-xxlarge <?php echo $validateClass; ?>"><?php echo htmlspecialchars($info['Value']); ?></textarea>
-
-
<?php endif; ?>
-
-
</td>
-
</tr>
-
<?php endforeach;?>
-
修改跟添加的模板就差一个value值
文章来源: blog.csdn.net,作者:咔咔-,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/fangkang7/article/details/85266648
- 点赞
- 收藏
- 关注作者
评论(0)