0

我有一个 cakedc 搜索插件,cakephp 3.0 工作正常,但希望有更高级的搜索过滤器,例如:

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 字段的下拉列表中。

=, !=, like %?%, like %?, not like ?% 条件将是一个下拉列表

将输入“洛杉矶”搜索值。

4

2 回答 2

0

想通了,可能并不完美,但似乎工作正常:

看法:

<?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);

}
于 2015-05-02T20:52:37.940 回答
0

我清理了代码,只是因为看起来代码很大。换句话说,为什么在条件选择输入中包含一个空选项?使用默认搜索条件看起来不是更好吗?萨卢多斯

于 2015-05-18T06:20:49.877 回答