Php OctoberCMS插件使用自定义查询扩展搜索过滤器
我创建了一个名为“Property”的插件,在清单中,我将多个地址字段显示在一个列中,该列将合并多个地址字段,如邮政编码、街道类型、街道号码等 我可以在清单中显示它们。以下是我迄今为止为实现这一目标所做的工作 plugins\technobrale\properties\models\property\columns.yamlPhp OctoberCMS插件使用自定义查询扩展搜索过滤器,php,octobercms,octobercms-backend,octobercms-plugins,Php,Octobercms,Octobercms Backend,Octobercms Plugins,我创建了一个名为“Property”的插件,在清单中,我将多个地址字段显示在一个列中,该列将合并多个地址字段,如邮政编码、街道类型、街道号码等 我可以在清单中显示它们。以下是我迄今为止为实现这一目标所做的工作 plugins\technobrale\properties\models\property\columns.yaml columns: id: label: Property Address type: property_address
columns:
id:
label: Property Address
type: property_address
searchable: true
sortable: false
plugins\technobrave\properties\Plugin.php
public function registerListColumnTypes()
{
return [
// A local method, i.e $this->evalUppercaseListColumn()
'property_address' => [$this, 'evalPropertydDetailsListColumn'],
];
}
public function evalPropertydDetailsListColumn($value, $column, $record)
{
$property_array_data = array();
$current_property = Property::where('id', $record->id)->first();
if($current_property)
{
if( ($current_property->lot != NULL) || ($current_property->lot != '') )
{
$property_array_data[] = $current_property->lot;
}
if( ($current_property->unit != NULL) || ($current_property->unit != '') )
{
$property_array_data[] = $current_property->unit;
}
if( ($current_property->street_number != NULL) || ($current_property->street_number != '') )
{
$property_array_data[] = $current_property->street_number;
}
if( ($current_property->po_box != NULL) || ($current_property->po_box != '') )
{
$property_array_data[] = $current_property->po_box;
}
if( ($current_property->street != NULL) || ($current_property->street != '') )
{
$property_array_data[] = $current_property->street;
}
if( ($current_property->street_type_id != NULL) || ($current_property->street_type_id != '') )
{
$get_street_type_data = StreetType::where('id', $current_property->street_type_id)->first();
$property_array_data[] = $get_street_type_data->street_name;
}
if( ($current_property->state_id != NULL) || ($current_property->state_id != '') )
{
$get_state_data = State::where('id', $current_property->state_id)->first();
$property_array_data[] = $get_state_data->state_name;
}
if( ($current_property->suburb_id != NULL) || ($current_property->suburb_id != '') )
{
$get_suburb_data = Suburb::where('id', $current_property->suburb_id)->first();
$property_array_data[] = $get_suburb_data->suburb;
}
if( ($current_property->post_code != NULL) || ($current_property->post_code != '') )
{
$property_array_data[] = $current_property->post_code;
}
$imp_property_data = implode(' ', $property_array_data);
return $imp_property_data;
}
}
当我在搜索框中搜索记录时,我只需要一个帮助就可以搜索上面的地址字段
有什么想法吗
谢谢您可以在
列中添加更多列。yaml
具有不可见:true
和可排序:true
。因此,您需要添加地块
,单元
,街道号
,等等
列:
地段:
标签:地段
类型:文本
可搜索:正确
隐形:真的
单位:
标签:单位
类型:文本
可搜索:正确
不可见:true
并更改list_config.yaml中的搜索策略
(…)
工具栏:
按钮:列表工具栏
搜索:
提示:“后端::lang.list.search\u提示”
模式:“任意”
好的,伙计们
最终我想出了一些办法来实现这一目标
来自plugins\technobrale\properties\models\property\columns.yaml和plugins\technobrale\properties\Plugin.php
public function registerListColumnTypes()
{
return [
// A local method, i.e $this->evalUppercaseListColumn()
'property_address' => [$this, 'evalPropertydDetailsListColumn'],
];
}
public function evalPropertydDetailsListColumn($value, $column, $record)
{
$property_array_data = array();
$current_property = Property::where('id', $record->id)->first();
if($current_property)
{
if( ($current_property->lot != NULL) || ($current_property->lot != '') )
{
$property_array_data[] = $current_property->lot;
}
if( ($current_property->unit != NULL) || ($current_property->unit != '') )
{
$property_array_data[] = $current_property->unit;
}
if( ($current_property->street_number != NULL) || ($current_property->street_number != '') )
{
$property_array_data[] = $current_property->street_number;
}
if( ($current_property->po_box != NULL) || ($current_property->po_box != '') )
{
$property_array_data[] = $current_property->po_box;
}
if( ($current_property->street != NULL) || ($current_property->street != '') )
{
$property_array_data[] = $current_property->street;
}
if( ($current_property->street_type_id != NULL) || ($current_property->street_type_id != '') )
{
$get_street_type_data = StreetType::where('id', $current_property->street_type_id)->first();
$property_array_data[] = $get_street_type_data->street_name;
}
if( ($current_property->state_id != NULL) || ($current_property->state_id != '') )
{
$get_state_data = State::where('id', $current_property->state_id)->first();
$property_array_data[] = $get_state_data->state_name;
}
if( ($current_property->suburb_id != NULL) || ($current_property->suburb_id != '') )
{
$get_suburb_data = Suburb::where('id', $current_property->suburb_id)->first();
$property_array_data[] = $get_suburb_data->suburb;
}
if( ($current_property->post_code != NULL) || ($current_property->post_code != '') )
{
$property_array_data[] = $current_property->post_code;
}
$imp_property_data = implode(' ', $property_array_data);
return $imp_property_data;
}
}
我只是将下面的代码放在columns.yaml文件中
plugins\technobrale\properties\models\property\columns.yaml
columns:
unit:
label: Property Address
type: text
searchable: true
sortable: false
invisible: true
select: CONCAT(COALESCE(`lot`,''),' ',COALESCE(`unit`,''),' ',COALESCE(`street_number`,''),' ',COALESCE(`po_box`,''),' ',COALESCE(`street`,''), ' ', COALESCE( (SELECT technobrave_streets_.street_name FROM technobrave_streets_ WHERE technobrave_streets_.id=street_type_id), ''), ' ', COALESCE( (SELECT technobrave_states_.state_name FROM technobrave_states_ WHERE technobrave_states_.id=state_id), ''), ' ', COALESCE( (SELECT technobrave_suburbs_.suburb FROM technobrave_suburbs_ WHERE technobrave_suburbs_.id=suburb_id), ''), ' ', COALESCE(`post_code`,'') )
正如您在上面代码中所看到的,现在我通过选择使用CONCAT
查询自身来获取属性地址,并将可搜索设置为true
,以使其可搜索
希望这有帮助 您不需要这一行:$current_property=property::where('id',$record->id)->first()
因为$record已经是您需要的模型了。问题是,当我尝试使用完整地址搜索时,它不起作用。。比如10(我的街道号码)Mcrain路(我的街道类型)。。它只适用于单柱。。如何通过键入完整地址和组合搜索使其工作。啊。。。好的,尝试用模式:“any”更改配置列表.yaml
中的搜索策略。我更新了我的答案。我已经添加了我的努力来解决这个问题,伙计。我感谢你的努力和时间。