CAKEPHP CAKEDC搜索插件按名称价值和条件进行过滤



我有一个带有cakephp 3.0的cakedc搜索插件,但是希望有更多高级搜索过滤器,例如:

city = 'Los Angeles';
city != 'Los Angeles';
city LIKE '%Angeles%';
city LIKE 'Los%';
city NOT LIKE '%Angeles%';
city NOT LIKE 'Los%';
etc...

所以我想添加2个下拉列表选择和1个文本输入来实现此目的。

'City'将在DB字段的下拉中。

=,!=,喜欢%?%,喜欢%?不喜欢?%条件是下拉级

'洛杉矶的搜索值将输入。

弄清楚,可能不是完美的,但似乎效果很好:

查看:

<?php echo $this->Form->input('dbfield', ['label' => 'Field', 'options' => [''  => '', 'region' => 'Region', 'city' => 'City', 'keyword' => 'Keyword']]); ?>
<?php echo $this->Form->input('dbconditions', [
    'label' => 'Condition',
    'options' => [
        ''  => '',
        'contains' => 'contains',
        'doesnotcontain' => 'does not contain',
        'beginswith' => 'begins with',
        'endswith' => 'ends with',
        'isequalto' => 'is equal to',
        'isnotequalto' => 'is not equal to',
    ],
]); ?>
<?php echo $this->Form->input('dbvalue', ['label' => 'Value', 'class' => 'form-control input-sm']); ?>

模型

public $filterArgs = [
    'dbfield' => [
        'type' => 'finder',
        'finder' => 'conds0',
    ],
    'dbconditions' => [
        'type' => 'finder',
        'finder' => 'conds0',
    ],
    'dbvalue' => [
        'type' => 'finder',
        'finder' => 'conds',
    ],
];
public function findConds0(Query $query, $options = []) {
}
public function findConds(Query $query, $options = []) {
    if($options['data']['dbconditions'] == 'contains') {
        $conditions = [
            $options['data']['dbfield'] . ' LIKE' => '%' . $options['data']['dbvalue'] . '%',
        ];
    }
    if($options['data']['dbconditions'] == 'doesnotcontain') {
        $conditions = [
            $options['data']['dbfield'] . ' NOT LIKE' => '%' . $options['data']['dbvalue'] . '%',
        ];
    }
    if($options['data']['dbconditions'] == 'beginswith') {
        $conditions = [
            $options['data']['dbfield'] . ' LIKE' => $options['data']['dbvalue'] . '%',
        ];
    }
    if($options['data']['dbconditions'] == 'endswith') {
        $conditions = [
            $options['data']['dbfield'] . ' LIKE' => '%' . $options['data']['dbvalue'],
        ];
    }
    if($options['data']['dbconditions'] == 'isequalto') {
        $conditions = [
            $options['data']['dbfield'] => $options['data']['dbvalue'],
        ];
    }
    if($options['data']['dbconditions'] == 'isnotequalto') {
        $conditions = [
            $options['data']['dbfield'] . ' != ' => $options['data']['dbvalue'],
        ];
    }
    return $query->where($conditions);
}

我清洁代码,只有beacuse看起来很大。换句话说,为什么在条件选择输入中包含一个空选项?默认搜索条件看起来不会更好?saludos

最新更新