2014-03-05 78 views
0

我的index.ctp具有包含2個輸入字段的搜索表單。一個是基於模型字段BillingCenter.name的名稱字段,另一個是未鏈接到模型字段的日期字段。 按搜索將通過URL將參數傳遞給「搜索」操作。CakePHP - 使用先前的搜索值填充搜索表單

index.ctp

echo $this->Form->create('BillingCenter', array('type' => 'get', 'action' => 'search')); 

echo $this->Form->input('BillingCenter.name'); 

echo $this->Form->input('date', array(
'type' => 'date', 
'label' => 'Date', 
'empty' => TRUE, 
'minYear' => 2000, 
'dateFormat' => 'DMY', 
'maxYear' => date('Y'), 
'minYear' => date('Y') - 10 
# default order m/d/y 
)); 

echo $this->Form->end('Search'); 

當我點擊搜索它產生不那麼好看的網址是這樣的:

http://localhost/bm/billing_centers/search?name=jo&date%5Bday%5D=01&date%5Bmonth%5D=03&date%5Byear%5D=2014 

這是在我的控制器簡單的「搜索」行動。

public function search() { 

    $this->view = 'index'; 

    if (!isset($this->request->query['name']) OR !isset($this->request->query['date'])) { 
     throw new BadRequestException(); 
    } 

    //concatenate date from URL      
    $effectivedate = $this->request->query['date']['year'] . $this->request->query['date']['month'] . $this->request->query['date']['day']; 

    //assemble the search conditions 
    $searchfilter = array(
         'BillingCenter.name LIKE' => '%' . $this->request->query['name'] . '%', 
         'BillingCenter.startdate <=' => $effectivedate, 
         'BillingCenter.enddate  >=' => $effectivedate 
         ); 

    $this->set('billingcenters', $this->BillingCenter->find('all', array('conditions' => $searchfilter ))); 

該搜索似乎工作正常(請評論,如果我以上做任何事情都是不好的做法)。 搜索引回相同的index.ctp並顯示正確的搜索結果。

不過,我需要在搜索表單輸入字段值設置爲默認在以前的搜索提交的值,我怎樣才能得到這些值(大概是從URL),並自動填充文本輸入日期輸入字段?

乾杯 凱文

+1

嘗試https://github.com/ CakeDC /搜索 - 它完成了上述所有操作,沒有任何開銷,並且以乾淨乾燥的方式進行。它還帶有應該在這裏使用的PRG模式,並保留所選的過濾器。 – mark

回答

1

我相信你應該能夠使用$this->params->query視圖基於查詢,設置默認值

echo $this->Form->create('BillingCenter', array('type' => 'get', 'action' => 'search')); 

echo $this->Form->input('BillingCenter.name', array(
    'value' => $this->params->query['name'] //set name param as default value 
)); 

echo $this->Form->input('date', array(
'type' => 'date', 
'selected' => $this->params->query['date'], //set date array as selected value 
'label' => 'Date', 
'empty' => TRUE, 
'minYear' => 2000, 
'dateFormat' => 'DMY', 
'maxYear' => date('Y'), 
'minYear' => date('Y') - 10 
# default order m/d/y 
)); 

echo $this->Form->end('Search'); 
0
public function search() { 
     if ($this->request->is('post') || $this->request->is('put')) { 
//   /pr($this->request->data); exit; 
      $condition = array(
       'User.role' => array('U', 'P'), 
       'User.user_status' => array('active', 'lead', 'inactive'), 
       'OR' => array(
        'PersonalInformation.first_name' => $this->request->data['User']['first_name'], 
        'PersonalInformation.last_name' => $this->request->data['User']['last_name'], 
        'PersonalInformation.primary_phone' => $this->request->data['User']['primary_phone'], 
        'PersonalInformation.dob' => $this->request->data['User']['dob'], 
        'User.email' => $this->request->data['User']['email'], 
       ) 
      ); 
     }