Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

后台模型增改查(Admin/Think)中添加int型外键和string型外键的支持。 #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions wwwroot/Application/Admin/Common/function.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ function get_attribute_type($type=''){
'editor' => array('编辑器','text NOT NULL'),
'picture' => array('上传图片','int(10) UNSIGNED NOT NULL'),
'file' => array('上传附件','int(10) UNSIGNED NOT NULL'),
//foreign_int/foreign_string 2016-06-14 start
'foreign_int' => array('int型外键','int(10) UNSIGNED NOT NULL'),
'foreign_string' => array('string型下拉列表','varchar(255) UNSIGNED NOT NULL'),
//foreign_int/foreign_string 2016-06-14 end
);
return $type?$_type[$type][0]:$_type;
}
Expand Down
123 changes: 123 additions & 0 deletions wwwroot/Application/Admin/Controller/ThinkController.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function lists($model = null, $p = 0){
//解析列表规则
$fields = array();
$grids = preg_split('/[;\r\n]+/s', trim($model['list_grid']));
$foreignFields = array();//foreign_int/foreign_string 2016-06-14
foreach ($grids as &$value) {
if(trim($value) === ''){
continue;
Expand All @@ -40,6 +41,16 @@ public function lists($model = null, $p = 0){
$val = explode(':', $value);
// 支持多个字段显示
$field = explode(',', $val[0]);

//foreign_int/foreign_string 2016-06-14 start
foreach($field as $onefield) {
if (stripos($onefield, '.foreign') !== false) {
$onefield = substr($onefield, 0, stripos($onefield, '.foreign'));
array_push($foreignFields, $onefield);
}
}
//foreign_int/foreign_string 2016-06-14 end

$value = array('field' => $field, 'title' => $val[1]);
if(isset($val[2])){
// 链接信息
Expand All @@ -53,6 +64,9 @@ public function lists($model = null, $p = 0){
}
foreach($field as $val){
$array = explode('|',$val);
if (stripos($array[0], '.foreign') !== false) {
$array[0] = substr($array[0], 0, stripos($array[0], '.foreign'));
}
$fields[] = $array[0];
}
}
Expand Down Expand Up @@ -130,6 +144,57 @@ public function lists($model = null, $p = 0){
$page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
$this->assign('_page', $page->show());
}

//检查field如果是select或foreign_int/foreign_string则显示对应文字 2016-06-14 start
if (!empty($fields)) {
$fieldCondition = '';
foreach ($fields as $key1 => $value1) {
$fieldCondition .= '"' . $value1 . '",';
}
$fieldCondition = substr($fieldCondition, 0, -1);
$myattributes = D('attribute')->field(array('name', 'title','type','extra'))->where('model_id=' . $model['id'] . ' and name in (' . $fieldCondition . ') ')->select();

if (!empty($myattributes)) {
foreach ($myattributes as $i => $attr) {
if ($attr['type'] == 'select') {
$select_extra = parse_field_attr($attr['extra']);
foreach ($data as $key => $dataValue) {
if (array_key_exists($dataValue[$attr['name']], $select_extra)) {
$data[$key][$attr['name']] = $select_extra[$dataValue[$attr['name']]];
}
}
} else if (in_array($attr['name'], $foreignFields)) {
$foreignKeyArray = array();
foreach ($data as $key => $dataValue) {
array_push($foreignKeyArray, $dataValue[$attr['name']]);
}
$foreignKeyString = implode(',', $foreignKeyArray);
$foreign_extra = parse_field_attr($attr['extra']);
$foreignTable = $foreign_extra['table'];
$foreignKeyField = $foreign_extra['key'];
$foreignValueField = $foreign_extra['value'];
if(!empty($foreignTable) && !empty($foreignKeyField) && !empty($foreignValueField)) {
$foreignValueArray = D($foreignTable)->field(array($foreignKeyField, $foreignValueField))
->where($foreignKeyField . ' in (' . $foreignKeyString . ')')
->select();
if(!empty($foreignValueArray)) {
$foreignMap = array();
foreach($foreignValueArray as $foreignValue) {
$foreignMap[$foreignValue[$foreignKeyField]] = $foreignValue[$foreignValueField];
}

foreach ($data as $key => $dataValue) {
if (array_key_exists($dataValue[$attr['name']], $foreignMap)) {
$data[$key][$attr['name'] . '.foreign'] = $foreignMap[$dataValue[$attr['name']]];
}
}
}
}
}
}
}
}
//检查field如果是select或foreign_int/foreign_string则显示对应文字 2016-06-14 end

$data = $this->parseDocumentList($data,$model['id']);
$this->assign('model', $model);
Expand Down Expand Up @@ -183,6 +248,21 @@ public function edit($model = null, $id = 0){
} else {
$fields = get_model_attribute($model['id']);

//foreign_int 和 foreign_string. 2016-06-14 start
if(!empty($fields)) {
foreach($fields as $key1 => $value1) {
if(!empty($value1)) {
foreach($value1 as $key2 => $value2) {
if($value2['type'] == 'foreign_int' || $value2['type'] == 'foreign_string') {
$foreign_extra = parse_field_attr($value2['extra']);
$fields[$key1][$key2]['foreign'] = $this->getForeignArray($foreign_extra['table'], $foreign_extra['key'], $foreign_extra['value']);
}
}
}
}
}
//foreign_int 和 foreign_string. 2016-06-14 end

//获取数据
$data = M(get_table_name($model['id']))->find($id);
$data || $this->error('数据不存在!');
Expand All @@ -209,8 +289,31 @@ public function add($model = null){
$this->error($Model->getError());
}
} else {

//接受初始化参数 2016-06-14 start
$data = array();
foreach ($_REQUEST as $key => $value) {
$data[$key] = $value;
}
$this->assign('data', $data);
//接受初始化参数 2016-06-14 end

$fields = get_model_attribute($model['id']);

//foreign_int 和 foreign_string. 2016-06-14 start
if(!empty($fields)) {
foreach($fields as $key1 => $value1) {
if(!empty($value1)) {
foreach($value1 as $key2 => $value2) {
if($value2['type'] == 'foreign_int' || $value2['type'] == 'foreign_string') {
$foreign_extra = parse_field_attr($value2['extra']);
$fields[$key1][$key2]['foreign'] = $this->getForeignArray($foreign_extra['table'], $foreign_extra['key'], $foreign_extra['value']);
}
}
}
}
}
//foreign_int 和 foreign_string. 2016-06-14 end

$this->assign('model', $model);
$this->assign('fields', $fields);
Expand Down Expand Up @@ -243,4 +346,24 @@ protected function checkAttr($Model,$model_id){
}
return $Model->validate($validate)->auto($auto);
}

/**
* 读取外键列表
* @param string $table
* @param string $keyField
* @param string $valueField
* @return multitype:Ambigous <>
* @author 王洋 <[email protected]>
*/
private function getForeignArray($table, $keyField, $valueField) {
if(empty($table) || empty($keyField) || empty($valueField)) {
return array();
}
$projectTypes = D($table)->select();
$result = array();
foreach($projectTypes as $i => $item) {
$result[$item[$keyField]] = $item[$valueField];
}
return $result;
}
}
7 changes: 7 additions & 0 deletions wwwroot/Application/Admin/View/Think/add.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ <h2>新增 [{$model['title']}]</h2>
</volist>
</select>
</case>
<case value="foreign_int|foreign_string">
<select id="{$field.name}" name="{$field.name}">
<volist name="field['foreign']" id="vo">
<option value="{$key}" <eq name="data[$field['name']]" value="$key">selected</eq>>{$vo}</option>
</volist>
</select>
</case>
<case value="radio">
<volist name=":parse_field_attr($field['extra'])" id="vo">
<label class="radio">
Expand Down
7 changes: 7 additions & 0 deletions wwwroot/Application/Admin/View/Think/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ <h2>编辑 [{$model['title']}]</h2>
</volist>
</select>
</case>
<case value="foreign_int|foreign_string">
<select id="{$field.name}" name="{$field.name}">
<volist name="field['foreign']" id="vo">
<option value="{$key}" <eq name="data[$field['name']]" value="$key">selected</eq>>{$vo}</option>
</volist>
</select>
</case>
<case value="radio">
<volist name=":parse_field_attr($field['extra'])" id="vo">
<label class="radio">
Expand Down