2011-01-27 162 views
0

我想知道什么最干净的方法是实现一个cakephp表单,其中1控件是多选,其余是文本字段或单选,然后数据是用saveall()插入多行。因此,例如,一个表格选择具有这些值:Cakephp:关于saveall()与多选的问题

文本框甲 值=美孚

多功能使用选乙 值= US,墨西哥,加拿大

单=选择C 值= 10

,所以我想用白水()插入这些行插入到数据库: 美孚,美国,10 富,墨西哥,10 富,加拿大,10

现在我知道在添加视图我可以使用这种格式输入语句:

输入(“Model.0.field1”,...)

,但我不知道是否我可以混合输入格式如 ('Model.field2',....)。

更新: 当我混搭单选和多选控件,表单数据被提交这样的:

Array 
(
    [Alert] => Array 
     (
      [schedule_id] => 75 
      [user_id] => 6 
      [0] => Array 
       (
        [frequency] => Array 
         (
          [0] => WEEKLY 
          [1] => MONTHLY 
         ) 

       ) 

      [limit_value] => .03 
      [limit_adjustment] => 0 
      [type] => LIMIT 
      [disabled] => 0 
     ) 

) 

我试图通过这些数据转化为白水(),但它把它就像一个单一的记录。

UPDATE2:我觉得白水()要求数据的多行这样的格式:

Array 
(
    [Article] => Array(
      [0] => Array 
       (
          [title] => title 1 
         ) 
      [1] => Array 
       (
          [title] => title 2 
         ) 
       ) 
) 

所以它看起来像后提交我会需要一些JavaScript代码,将重组阵列。

回答

0

我有一些工作......我不确定它是否充分利用了所有蛋糕的“automagic”功能,但我不认为它太复杂。

所以我只是下面的代码添加到我的控制器的add函数:

if (!empty($this->data)) { 
      //debug($this->data, true); 

         /* begin custom code */ 
      $multiselect = $this->data['Alert']['entity_id']; 

      $tmp2 = array(); 
      foreach ($multiselect as $item) 
      { 
       $tmp = $this->data['Alert']; 
       $tmp['entity_id'] = $item; 
       array_push($tmp2,$tmp); 

      } 

      $this->data['Alert'] = $tmp2; 

      debug($this->data,true); 
      /* end custom code */ 

      $this->Alert->create(); 

      //restructure data 





      if ($this->Alert->saveAll($this->data['Alert'])) { 
       $this->Session->setFlash(__('The alert has been saved', true)); 
       //$this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The alert could not be saved. Please, try again.', true)); 
     } 

,我的数据转换成这样:

Array 
(
    [Alert] => Array 
     (
      [0] => Array 
       (
        [schedule_id] => 74 
        [entity_id] => 1 
        [user_id] => 6 
        [frequency] => HOURLY 
        [limit_value] => .02 
        [limit_adjustment] => 0 
        [type] => LIMIT 
        [disabled] => 1 
       ) 

      [1] => Array 
       (
        [schedule_id] => 74 
        [entity_id] => 2 
        [user_id] => 6 
        [frequency] => HOURLY 
        [limit_value] => .02 
        [limit_adjustment] => 0 
        [type] => LIMIT 
        [disabled] => 1 
       ) 

     ) 

)