CakePHP搜索插件,搜索一个等于或高于搜索中给定的值 [英] CakePHP Search Plugin, search for a value equal or higher than the given in the search

查看:129
本文介绍了CakePHP搜索插件,搜索一个等于或高于搜索中给定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CakePHP搜索插件,有一些我不能工作。我想有一个搜索字段(让我们称之为 age ),用户可以在其中输入值(例如24),并获取所有具有 >年龄的年龄在24岁或以上的表格。

I'm using the CakePHP Search Plugin, and there's something I can't get to work. I would like to have a search field (let's call it age) where a user can input a value (like 24, for example) and get as a result all the elements with a value in the age field of the table who have age 24 or higher. How can I do that?

到目前为止,我可以执行正常搜索,如通过名称或确切的值(如果你想要24岁

So far I can perform normal searches, like by name or by an exact value (if you want age 24 exactly then it works fine).

以下是与我迄今为止的搜索插件相关的代码:

Here's the code related with the Search Plugin that I have so far:

- 模型:

class Person extends AppModel{ 

    var $name = 'Person';
    public $actsAs = array('Searchable');

    public $filterArgs = array(
        array('name' => 'name', 'type' => 'like'),
        array('name' => 'age', 'type' => 'value'),
    );
}

- 控制器:
$ b

- controller:

class PersonsController extends AppController {

var $name = 'Persons';
var $components = array('Search.Prg'); 
public $presetVars = array(
    array('field' => 'name', 'type' => 'value'),
    array('field' => 'age', 'type' => 'value'),
    );

function index() {
    $this->set('persons', $this->Person->find('all'));
}

/* ---------------- SEARCH PLUGIN --------------*/

public function find() {

    $this->Prg->commonProcess();
    $this->paginate['conditions'] = this->Game->parseCriteria($this->passedArgs);
    $this->set('persons', $this->paginate());
}   
}

- find.ctp:

//apart from the code below, I have all the code to show the results
    echo $form->create('Game', array(
        'url' => array_merge(array('action' => 'find'), $this->params['pass'])
        ));
    echo $form->input('name', array('div' => false));
    echo $form->input('age', array('div' => false));
    echo $form->submit(__('Search', true), array('div' => false));
    echo $form->end();

总结: 年龄字段的确切值。如何更改它以执行搜索age> = 输入的值

To sum up: with the code above, I can perfomr a search of an exact value for the age field. How do I change that to perform a search of 'age >= value entered'?

>

推荐答案

所以这是我解决它(或至少它似乎工作与我迄今为止所做的测试):

So this is how I solved it (or at least it seems to work with all the test I've done so far):


  • 在模型中的 filterArgs 数组中,将年龄行更改为以下行:

  • Changed the 'age' line by the following line in the filterArgs array in the model:

array('name' => 'age', 'type' => 'query', 'method' => 'findByAge', 'field' => 'Person.age'),


  • 在同一模型中添加以下方法:

  • Added the following method in the same model:

    public function findByAge($data = array()) {
    
        $query = array(
                       'Person.age >='  => $data['age'],
                        );
        return $query;
    }
    


  • 就是这样!使用查询类型,可以使用方法将任何复杂查询添加到主查询。感谢satyrwilder指向正确的方向!

    That's it! With the 'query' type, any complex query can be added to the main query using a method. Thanks to satyrwilder for pointing in the right direction!

    这篇关于CakePHP搜索插件,搜索一个等于或高于搜索中给定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆