0

我有一个devices/index页面,默认情况下我不想显示“乱序”设备,除非我标记了一个正确的复选框

所以我使用了 CakeDC 搜索插件 3,然后我编辑了

设备表

 public $filterArgs = [


     'include_out_of_order' => [
        'type' => 'finder', 
        'finder' => 'outOfOrder',
        'allowEmpty' => true
    ],  

     // ...
     // lot of other filters
     // ...

 ]


public function findOutOfOrder($query, array $options)
{
    if(isset($options['include_out_of_order']) &&  $options['include_out_of_order'] == true)
        return $query;   
    else
        return $query->where(['Devices.device_status_id !=' => 2]);  //status = 2 means the device is out of order

}

现在这在我index看来有效,但它也适用于我使用搜索插件过滤记录的另外两个操作。

我希望这个特定的过滤器只在index行动中应用,而所有其他过滤器应该在设备控制器的其他操作中工作

是否有另一种方法可以使用 cakeDC 插件来实现我想要做的事情,或者我应该index在控制器内的操作中使用一些自定义代码?这很容易,但我想找到一个更干净的解决方案

4

1 回答 1

0

我希望此行为仅适用于索引操作,而不是每次我应用过滤器时

在您的控制器中:

if ($this->request->action === 'index') {
    $this->loadComponent('Search.Prg');
}

不过,我会推荐FoC Search 插件。它有更好的架构,更适合 Cake3。它可以做同样的事情,但代码比 CDC 插件少得多。

于 2015-10-14T13:24:21.850 回答