【问题标题】:OctoberCMS plugin extend search filter with custom queryOctoberCMS 插件使用自定义查询扩展搜索过滤器
【发布时间】:2017-06-01 08:54:06
【问题描述】:

我创建了一个插件,将其称为“属性”,其中在列表中,我将多个地址字段显示在一列中,该列将组合多个地址字段,如邮政编码、街道类型、街道号等..

而且我可以在列表中显示它们。下面是我到目前为止为实现它所做的工作。

plugins\technobrave\properties\models\property\columns.yaml

columns:
    id:
        label: Property Address
        type: property_address
        searchable: true
        sortable: false    

插件\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;
    }









}

当我在搜索框中搜索记录时,我只需要帮助才能搜索上述地址字段..

有什么想法吗?

谢谢

【问题讨论】:

    标签: php octobercms octobercms-backend octobercms-plugins


    【解决方案1】:

    您可以使用invisible: truesorteable: truecolumns.yaml 中添加更多列。所以需要添加lotunitstreet_number

    columns: lot: label: lot type: text searchable: true invisible: true unit: label: unit type: text searchable: true invisible: true

    并更改list_config.yaml中的搜索策略

    (...) toolbar: buttons: list_toolbar search: prompt: 'backend::lang.list.search_prompt' mode: 'any'

    【讨论】:

    • 你不需要这行:$current_property = Property::where('id', $record->id)->first(); 因为 $record 已经是你需要的模型了。
    • 最重要的是,当我尝试使用完整地址进行搜索时,它不起作用.. 就像 10(我的街道号码) Mcrain 路(我的街道类型).. 它仅适用于单列..我如何通过组合搜索键入完整地址来使其工作。
    • 啊...好的,尝试将config_list.yaml 中的搜索策略更改为mode: 'any'。我更新了我的答案。
    • 我已经努力解决这个问题了,哥们。感谢您的努力和时间。
    【解决方案2】:

    好吧,伙计们,

    最终我想出了一些办法来实现这一目标。

    来自 plugins\technobrave\properties\models\property\columns.yamlplugins\technobrave\properties\Plugin.php

    我只是将下面的代码放在我的 columns.yaml 文件中。

    plugins\technobrave\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`,'') )
    

    正如您在上面的代码中看到的,现在我通过SELECT 使用CONCAT 查询本身获取属性地址,并将searchable 设置为true 以使其可搜索。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-03-09
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      • 2014-12-01
      • 1970-01-01
      • 2010-09-30
      • 2017-07-13
      • 2015-09-11
      相关资源
      最近更新 更多