2011-07-26 49 views
0

我想使用cakephp search plugin,我在使用查询方法过滤结果时遇到问题。我创建了一个名为findByLength()的函数,它在$ filterArgs数组中声明。我不确定我正在建设的阵列是否正确。当我查看sql语句时,它看起来好像没有被调用。有任何想法吗?谢谢!Cakephp(CakeDC)搜索插件自定义过滤方法

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

    } 

?> 

回答

1

原来我引用了错误的数组键。

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

和改变

public function findByLength(){ 

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