2012-11-15 79 views
0

我有一個cakephp 1.3應用程序,它獲得一個人的同意參與研究。我試圖在添加研究頁面上將研究的納入標準和排除標準關聯起來。 HABTM關係設置和工作(如果只有一種方法可以選擇)我現在要做的是將當前的包含條件顯示爲添加研究的人員可以選擇的複選框列表,但是我也想要一個字段新標準可以作爲逗號分隔列表輸入。現在它將保存複選框,但我希望它也添加在該字段中輸入的文本。屏幕上顯示添加研究現在位於下方。我真的不知道我會怎麼做。有沒有辦法做到這一點,或者我將不得不採取輸入的數據,整理它,然後將其添加到$ this-> data [包含字段數據]?一個例子會很好。 :)我已經添加了圖片下面的代碼。 Add Study ScreenCakephp 1.3從列表中選擇字段選項或輸入新的

控制器:

/** 
    * Add a study. 
    */ 
    function add() { 
    if (!empty($this->data)) { 

     //get the inclusions from the text data 
     //Sort through the comma seperated list and add it to $this->data['Study']['Inclusions']??? 

     $this->Study->create(); 
     if ($this->Study->save($this->data)) { 
     $this->Session->setFlash(__('The study has been saved', true)); 
     $this->redirect(array('action' => 'index')); 
     } else { 
     $this->Session->setFlash(__('The study could not be saved. Please, try again.', true)); 
     } 
    } 
    $this->set('inclusions', $this->Study->Inclusion->find('list', array(
     'fields' => array('id', 'name'), 
     'order' => 'Inclusion.name', 
     'recursive' => 0, 
    ))); 
    $this->set('exclusions', $this->Study->Exclusion->find('list', array(
     'fields' => array('id', 'name'), 
     'order' => 'Exclusion.name', 
     'recursive' => 0, 
    ))); 
    $this->set('forms', $this->Study->Form->find('list', array('order' => 'Form.name','recursive' => 0,))); 
    } 

查看:

<div class="studies form"> 
<?php echo $this->Form->create('Study', array('type' => 'file'));?> 
    <fieldset> 
    <legend><?php __('Add Study'); ?></legend> 
    <?php 
    echo $this->Form->input('studyname', array('label'=>'Study Name','required' => true)); 
    echo $this->Form->input('studynumber', array('label'=>'Study Number','required' => true)); 
    echo $this->Form->input('file', array('label'=>'Select Electronic Consent','type' => 'file')); 
    echo $this->Form->input('consent_form_date'); 
    ?> 
    <fieldset class="inclusion"> 
    <legend><?php __('Inclusions'); ?></legend> 
    <div class="IncExc"> 
    <?php 
    echo $this->Form->input('Inclusion',array(
     'label' => false, 
     'type' => 'select', 
     'multiple' => 'checkbox', 
     'options' => $inclusions, 
     'selected' => $html->value('Inclusion.Inclusion'), 
    )); 
    ?> 
    </div> 
    <?php 
    echo $form->input('Inclusion.inclusions',array(
      'type' => 'text', 
      'label' => __('Add New Inclusions',true), 
      'after' => __('Seperate each new Inclusion with a comma. Eg: family, sports, icecream',true) 
    )); 
    ?> 
    </fieldset> 
    <fieldset> 
    <legend><?php __('Exclusions'); ?></legend> 
    <div class="IncExc"> 
    <?php 
    echo $this->Form->input('Exclusion',array(
     'label' => false, 
     'type' => 'select', 
     'multiple' => 'checkbox', 
     'options' => $exclusions, 
     'selected' => $html->value('Exclusion.Exclusion'), 
    )); 
?> 
    </div> 
    </fieldset> 
    <fieldset style="width: 700px;"> 
    <legend><?php //__('Forms'); ?></legend> 
    <?php /* 
    echo $this->Form->input('Form',array(
     'label' => false, 
     'type' => 'select', 
     'multiple' => 'checkbox', 
     'options' => $forms, 
     'selected' => $html->value('Form.Form'), 
    )); 
*/ ?> 
    </fieldset> 
    </fieldset> 
<?php echo $this->Form->end(__('Submit', true));?> 
</div> 
+0

所以你想將逗號分隔列表轉換爲HABTM項目?如果是這樣,就你的解析它的評論,並將其存儲在數組中,然後保存,你就在正確的軌道上。沒有「簡單」的方式。 – Dave

+0

@Dave是的,我試圖做到這一點,同時保持用戶選擇的HABTM項目。所以存儲的是用戶選擇的項目+用戶輸入的項目。你能舉一個例子說明這個語法是什麼嗎?將排序後的列表添加到選定列表中?我可以解析它,我只是不知道如何將它添加到已經存在的/被選中的。 – Jonathan

+0

只是從提交中調試()你在Controller中收到的數據,並與之匹配。 Bahaha, – Dave

回答

1

你正在嘗試做的幾乎是相同的,因爲他想做的事:Saving tags into a database table in CakePHP

,你所要做的是:

  1. 檢查手動包含字段是否爲空。如果該字段爲空,則忽略該字段並轉至步驟5
  2. 如果該字段不爲空,請將其分隔爲逗號或任何要用作分隔符的標誌。你現在可以驗證它,如果你想。
  3. 使用第3步的所有內容並將它們保存在自定義或真正的包含表中。你應該以某種方式跟蹤這些自定義條目,所以你不要把它們與你提供的那些搞混。保存在數組中保存的所有ID。
  4. 將第3步收集到的ID與您從表單中獲得的ID合併。
  5. 保存所有收集到的和給定的數據到數據庫

上面包括代碼有可以用來做定後的的answere。你應該與它相處得很好,如果你讀了整個帖子+代碼註釋;)

如果您有任何問題,只是問;)

問候 func0der

+0

。這在我的任何搜索中都沒有出現。工作,但感謝func0der! – Jonathan

+0

完全沒問題;) – func0der

相關問題