2013-02-18 64 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