我有一個cakephp 1.3應用程序,它獲得一個人的同意參與研究。我試圖在添加研究頁面上將研究的納入標準和排除標準關聯起來。 HABTM關係設置和工作(如果只有一種方法可以選擇)我現在要做的是將當前的包含條件顯示爲添加研究的人員可以選擇的複選框列表,但是我也想要一個字段新標準可以作爲逗號分隔列表輸入。現在它將保存複選框,但我希望它也添加在該字段中輸入的文本。屏幕上顯示添加研究現在位於下方。我真的不知道我會怎麼做。有沒有辦法做到這一點,或者我將不得不採取輸入的數據,整理它,然後將其添加到$ this-> data [包含字段數據]?一個例子會很好。 :)我已經添加了圖片下面的代碼。 Cakephp 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>
所以你想將逗號分隔列表轉換爲HABTM項目?如果是這樣,就你的解析它的評論,並將其存儲在數組中,然後保存,你就在正確的軌道上。沒有「簡單」的方式。 – Dave
@Dave是的,我試圖做到這一點,同時保持用戶選擇的HABTM項目。所以存儲的是用戶選擇的項目+用戶輸入的項目。你能舉一個例子說明這個語法是什麼嗎?將排序後的列表添加到選定列表中?我可以解析它,我只是不知道如何將它添加到已經存在的/被選中的。 – Jonathan
只是從提交中調試()你在Controller中收到的數據,並與之匹配。 Bahaha, – Dave