【发布时间】:2011-06-20 22:41:14
【问题描述】:
我正在使用 CakePHP 搜索插件,但有些东西我无法工作。我想有一个搜索字段(我们称之为 age),用户可以在其中输入一个值(例如 24)并获得所有在 中具有值的元素>age 表中年龄为 24 岁或更高的字段。我该怎么做?
到目前为止,我可以执行正常搜索,例如按姓名或按确切值(如果您希望 准确 24 岁,那么它可以正常工作)。
这是我目前拥有的与搜索插件相关的代码:
- 型号:
class Person extends AppModel{
var $name = 'Person';
public $actsAs = array('Searchable');
public $filterArgs = array(
array('name' => 'name', 'type' => 'like'),
array('name' => 'age', 'type' => 'value'),
);
}
- 控制器:
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 字段的确切值。如何更改它以执行“年龄 >= 输入值”的搜索?
非常感谢您!
【问题讨论】: