【问题标题】:cakedc search plugin search in price range价格范围内的 cakedc 搜索插件搜索
【发布时间】:2014-07-27 11:33:23
【问题描述】:

我正在尝试使用 cakedc 搜索插件在两个价格之间进行搜索,这是我的型号代码:

public $actsAs = array(
    'Search.Searchable'
);

public $filterArgs = array(
    'price' => array(
    'type' => 'expression',
    'method' => 'makeRangeCondition',
    'field' => 'Price.views BETWEEN ? AND ?'
    )
);

   public function makeRangeCondition($data = array()) {
    if (strpos($data['price'], ' - ') !== false){
        $tmp = explode(' - ', $data['price']);
        $tmp[0] = $tmp[0] ;
        $tmp[1] = $tmp[1] ;
        return $tmp;
    } else {
        return array($minPrice, $maxPrice) ;
    }
}

我的控制器的代码:

public function index() {
    $this->Prg->commonProcess();
    $cond = $this->Property->parseCriteria($this->passedArgs);

$this->set('properties', $this->paginate('Property', $cond));
}

我的观点代码:

<?php
echo $this->Form->create();
echo $this->Form->input('minPrice');
echo $this->Form->input('maxPrice');
echo $this->Form->submit(__('Submit'));
echo $this->Form->end();

?>

表 sql:

CREATE TABLE IF NOT EXISTS `properties` (

id varchar(36) NOT NULL, price 浮动默认空值, 主键 (id), 唯一键 ID (id) ) ENGINE=MyISAM 默认字符集=utf8;

【问题讨论】:

  • 感谢 Chris 我已经尝试使用“like”搜索没有任何问题,使用此代码 {public $actsAs = array('Search.Searchable'); public $filterArgs = array('price' => array('type' => 'like', 'field' => 'price'));}
  • 恐怕我帮不了你。但是如果你添加更多合适的标签,你会得到更多的用户关注。

标签: php function cakephp search cakedc


【解决方案1】:

而不是 filterArgs 中的“范围”键,将其命名为“价格”。因为只有当 data[key] 不为空时,插件才会检查键名和调用方法。

【讨论】:

  • 谢谢我已经更新了我所有的代码仍然没有返回任何东西
【解决方案2】:

如果您使用两个输入,您应该在 $filterArgs 数组中使用两个参数。

您可以在您的模型中尝试此代码:

public $filterArgs = array(
        'minPrice' => array(
            'type' => 'query',
            'method' => 'priceRangeMin'
        ),

        'maxPrice' => array(
             'type' => 'query',
             'method' => 'priceRangeMax'
        )
    );

public function priceRangeMin( $data = array() ) {
    if( !empty( $data['minPrice'] ) ) {
        return array('Property.price >= '.$data['minPrice'].'');
    }
}

public function priceRangeMax( $data = array() ) {
    if( !empty( $data['maxPrice'] ) ) {
        return array('Property.price <= '.$data['maxPrice'].'');
    }
}

在你的控制器和视图中也使用相同的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    相关资源
    最近更新 更多