2012-11-15 24 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

相关问题