2013-02-18 63 views
0

我正在使用搜索插件https://github.com/cakedc/search。我正在尝试仅使用ID字段搜索记录,也就是说,如果我在表单输入中输入1,它必须显示带有用户标识1的记录。我现在面临的挑战是当我指定输入应该是id字段,搜索功能的输入字段在我的索引视图中消失,奇怪的是当我指定输入字段显示的不同字段名称时。搜索功能无法使用ID字段输入

下面是我为我的模型

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

public $filterArgs = array(
'id' => array('type' => 'like', 'field' => 'ItSupportRequest.id'), 
); 

public function findByTags($data = array()) { 
$this->Tagged->Behaviors->attach('Containable', array('autoFields' => false)); 
$this->Tagged->Behaviors->attach('Search.Searchable'); 
$query = $this->Tagged->getQuery('all', array(
'conditions' => array('Tag.name' => $data['tags']), 
'fields' => array('foreign_key'), 
'contain' => array('Tag') 
)); 
return $query; 
} 

public function orConditions($data = array()) { 
$filter = $data['filter']; 
$cond = array(
'OR' => array(
$this->alias . '.id LIKE' => '%' . $filter . '%', 
)); 
return $cond; 

}

这里是我的控制器代码的代码。

public $components = array('Search.Prg'); 

public $presetVars = true; // using the model configuration 

public function find() { 
$this->Prg->commonProcess(); 
$this->paginate['conditions'] = $this->ItSupportRequest->parseCriteria($this->passedArgs); 
$this->set('articles', $this->paginate()); 
} 

和我index.ctp文件我有这样的代码。

<?php 
echo $this->Form->create('ItSupportRequest', array(
'url' => array_merge(array('action' => 'find'), $this->params['pass']) 
)); 
echo $this->Form->label('Query ID:') . "<br/>";   
echo $this->Form->input('name', array('div' => false, 'label' => false)); 

echo $this->Form->submit(__('Search'), array('div' => false)); 
echo $this->Form->end(); 
?> 

在此先感谢。

+0

我不看到任何“ID” - 只有“名称” – mark 2013-02-18 14:37:43

回答

0

你不应该把它叫做id,因为id会被隐藏(因为cake认为这是主键)。

要么将​​其命名为别的东西或手动改写使用

$this->Form->input('id', array('type' => 'text')); 

这种行为,但我会用某物去像“搜索”和

$this->Form->input('search', array('placeholder' => 'ID to look for')); 

public $filterArgs = array(
    'search' => array('type' => 'like', 'field' => 'ItSupportRequest.id'), 
); 
+0

如果我把一个不存在的ID显示我错误的结果 – Mhofhands 2013-02-18 14:33:09

+0

你是什么意思与错误?还有,为什么使用像ID一样? ID是值 - 所以在这里使用没有意义。 – mark 2013-02-18 14:37:03

+0

如果您使用“搜索”作为表单字段,则需要在filterArgs“seach”中输入您的密钥。 – mark 2013-02-18 14:47:50