0

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 フィールドのドロップダウンに含まれます。

=, !=, 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 に答える