联系官方销售客服
1835022288
028-61286886
分享内容:《如何在linkage中实现自定义字段的搜索》
实现功能:多个条件匹配,目前只能匹配等于
代码 如下:
case 'linkage': // 联动菜单
$linkage = \Phpcmf\Service::L('cache')->get('linkage-' . $system['site'] . '-' . $param['code']);
if (!$linkage) {
return $this->_return($system['return'], "联动菜单{$param['code']}不存在,请在后台更新缓存");
}
// 通过别名找id
$ids = @array_flip(\Phpcmf\Service::C()->get_cache('linkage-' . $system['site'] . '-' . $param['code'] . '-id'));
if (isset($param['pid'])) {
if (is_numeric($param['pid'])) {
$pid = intval($param['pid']);
} elseif (!$param['pid']) {
$pid = 0;
} else {
$pid = isset($ids[$param['pid']]) ? $ids[$param['pid']] : 0;
!$pid && is_numeric($param['pid']) && \Phpcmf\Service::C()->get_cache('linkage-' . $system['site'] . '-' . $param['code'] . '-id', $param['pid']) && $pid = intval($param['pid']);
}
}
$i = 0;
$return = array();
foreach ($linkage as $t) {
if ($system['num'] && $i >= $system['num']) {
break;
} elseif (isset($param['pid']) && $t['pid'] != $pid) {
continue;
} elseif (isset($param['id']) && !in_array($t['id'], explode(',', $param['id']))) {
continue;
}
$return[] = $t;
$i++;
}
if (!$return && isset($param['pid'])) {
$rpid = isset($param['fid']) ? (int)$ids[$param['fid']] : (int)$linkage[$param['pid']]['pid'];
foreach ($linkage as $t) {
if ($t['pid'] == $rpid) {
if ($system['num'] && $i >= $system['num']) {
break;
}
if (isset($param['id']) && !in_array($t['id'], explode(',', $param['id']))) {
continue;
}
$return[] = $t;
$i++;
}
}
if (!$return) {
return $this->_return($system['return'], '没有匹配到内容');
}
}
/**
* 其他自定义参数实现开始
*/
//获取当前linkage的所有字段
$db_field_sources = $return[0];
$linkage_db_field = [];
foreach ($db_field_sources as $field => $val) {
array_push($linkage_db_field, $field);
}
$customer_params = array();
if (count($param) > 0) {
foreach ($param as $key => $val) {
if ($key == 'code' || $key == 'pid' || $key == 'id') {
continue;
}
if (!in_array($key, $linkage_db_field)) {
continue;
}
$customer_params[$key] = $val;
}
}
if (!empty($customer_params) && count($customer_params) > 0) {
foreach ($return as $key => $item) {
foreach ($customer_params as $filter_field => $filter_value) {
if($item[$filter_field]!=$filter_value){
unset($return[$key]);
}
}
}
}
/**
* 其他自定义参数实现结束
*/
return $this->_return($system['return'], isset($param['call']) && $param['call'] ? @array_reverse($return) : $return, '');
break;