Cakephp(CakeDC)搜索插件自定义过滤方法

发布于 2024-11-26 14:52:44 字数 2509 浏览 1 评论 0原文

我正在尝试使用 cakephp 搜索插件,但在使用查询方法过滤结果时遇到问题。我创建了一个名为 findByLength() 的函数,该函数在 $filterArgs 数组中声明。我不确定我正在构建的数组是否正确。当我查看 sql 语句时,它看起来甚至没有被调用。有什么想法吗?谢谢!

<?php
// findByLength() is not affecting the sql like expected. Any ideas?
// using http://cakedc.com/downloads/view/cakephp_search_plugin

    public $filterArgs = array(
            array('name' => 'name', 'type' => 'like'),
            array('name' => 'search', 'type' => 'like', 'field' => 'Trail.description'),
            array('name' => 'type','type'=>'string'),
            array('name'=>'dogs_allowed','type'=>'value'),
            array('name'=>'area_id','type'=>'value'),
            array('name' => 'length', 'type' => 'query', 'method' => 'findByLength', 'field' => 'Trail.length'),
            array('name' => 'state_id','field'=>'Area.state_id', 'type' => 'value'),
            array('name'=>'created_by_pt','field'=>'Trail.created_by','type'=> 'value'),
            //array('name'=>'created_by_user','field'=>'Trail.created_by','type'=> 'value'),
 );
    public function findByLength(){

        if(empty($data['Trail']['length'])){
            return array();
        }
        switch($data['Trail']['length']){
            case 0:
                return array('AND'=> array($this->alias.'.length >='=> $data['Trail']['length'],
                                                                     $this->alias.'.length <'=> $data['Trail']['length'] + 3)

                                );
            break;
            case 3:
                return array('AND'=> array($this->alias.'.length >='=> $data['Trail']['length'],
                                                                     $this->alias.'.length >'=> $data['Trail']['length'] + 3)

                                );
            break;
            case 6:
            return array('AND'=> array($this->alias.'.length >='=> $data['Trail']['length'],
                                                                 $this->alias.'.length <'=> $data['Trail']['length'] + 4)

                            );
            break;
            case 10:
            return array('AND'=> array($this->alias.'.length >='=> $data['Trail']['length'])

                            );
            break;
        }

    }

?>

I am trying to use the cakephp search plugin and I am having problems using the query method to filter results. I created a function called findByLength() which is declared in the $filterArgs array. I'm not sure if the array i'm building out is right. When I look at the sql statements it looks like its not even being invoked. Any ideas? Thanks!

<?php
// findByLength() is not affecting the sql like expected. Any ideas?
// using http://cakedc.com/downloads/view/cakephp_search_plugin

    public $filterArgs = array(
            array('name' => 'name', 'type' => 'like'),
            array('name' => 'search', 'type' => 'like', 'field' => 'Trail.description'),
            array('name' => 'type','type'=>'string'),
            array('name'=>'dogs_allowed','type'=>'value'),
            array('name'=>'area_id','type'=>'value'),
            array('name' => 'length', 'type' => 'query', 'method' => 'findByLength', 'field' => 'Trail.length'),
            array('name' => 'state_id','field'=>'Area.state_id', 'type' => 'value'),
            array('name'=>'created_by_pt','field'=>'Trail.created_by','type'=> 'value'),
            //array('name'=>'created_by_user','field'=>'Trail.created_by','type'=> 'value'),
 );
    public function findByLength(){

        if(empty($data['Trail']['length'])){
            return array();
        }
        switch($data['Trail']['length']){
            case 0:
                return array('AND'=> array($this->alias.'.length >='=> $data['Trail']['length'],
                                                                     $this->alias.'.length <'=> $data['Trail']['length'] + 3)

                                );
            break;
            case 3:
                return array('AND'=> array($this->alias.'.length >='=> $data['Trail']['length'],
                                                                     $this->alias.'.length >'=> $data['Trail']['length'] + 3)

                                );
            break;
            case 6:
            return array('AND'=> array($this->alias.'.length >='=> $data['Trail']['length'],
                                                                 $this->alias.'.length <'=> $data['Trail']['length'] + 4)

                            );
            break;
            case 10:
            return array('AND'=> array($this->alias.'.length >='=> $data['Trail']['length'])

                            );
            break;
        }

    }

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝色星空 2024-12-03 14:52:44

事实证明我引用了错误的数组键。

$data['Trail']['length'] ended up being $data['length']

public function findByLength(){

改为

public function findByLength($data = array()){

It turns out I was referencing the wrong array keys.

$data['Trail']['length'] ended up being $data['length']

and changed

public function findByLength(){

to

public function findByLength($data = array()){
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文