2011-07-12 46 views
0

表單(newreports.php)在提交時需要保存到表中,並將隱藏數據(每條記錄有7列的27條記錄)保存到BelongsTo表中。 7列是:id,user_id,reports_id,count,area,area_id,comments。區域需要預填充爲0-26,並且reports_id對於所有(100)需要相同。 User_id應該從表單條目中預先填寫。另外,id應該自動填充。我認爲這可以在我的控制器中爲函數newreports()完成。如何在cakePHP中保存多個表屬於?

我需要寫這樣的數組還是有簡化的方法?

$this->Report->saveAll(
    Array 
(
    [Report] => Array 
     (
      [0] => Array 
       (
        [id] => //leave blank because it will auto-fill? 
      [user_id] => //dynamically from form input 
        [reports_id] => //dynamically from form input 
        [area_id] => //dynamically from form input 
        [area] => 0 
    [count] => // this should be blank as there are no counts yet 
    [comments] => // this should be blank as there are no comments yet 
       ) 
     [1] => Array 
       (
        [id] => //leave blank because it will auto-fill? 
      [user_id] => //dynamically from form input 
        [reports_id] => //dynamically from form input 
        [area_id] => //dynamically from form input 
        [area] => 1 
    [count] => // this should be blank as there are no counts yet 
    [comments] => // this should be blank as there are no comments yet 
       ) 
     ) 
) 

回答

0

如果表單/視圖代碼被寫入正確的方式,你應該能夠$this->Report->saveAll($this->data['Report']);脫身。

查看錶單的HTML源代碼。名稱的值輸入標籤的屬性應該是這樣的data[Report][0][id]data[Report][0][user_id],等第二個記錄應具有領域看起來像data[Report][1][id]data[Report][2][user_id]

當這個被貼了,它會自動導入到正確的$ this-> data中的數組結構。

如果此表單將始終創建新記錄,那麼將data[Report][][id]字段省略(或將它們留空),並且saveAll將創建新記錄。


處理第一條評論的更新。

在控制器保存所有隱藏數據之前或之後,您是否想要分配$ data數據尚不清楚。我們來處理這兩個部分;首先,保存。因此,your own answer差不多可以創建數據。

$count = 27; 
$v = 0; 
$data = array('Report' => array()); 
do { 
    $data['Report'][] = array('reports_id' => $id, 'area' => $v); 
    $v++; 
} while ($v < $count); 

// save report rows to database 
$this->Report->saveAll($data['Report']); 

因此,它準備了一個數據結構,就像您在原始問題中所做的一樣,並在您的答案中。但是,我們使用saveAll()一次性創建它們。

接下來的部分是檢索保存的數據並放入$ this-> data中,這樣您就可以按照您的評論以用戶在重定向之後看到的形式使用它。在控制器中你需要這樣的東西。

$reports = $this->Report->find('all', array(
    'conditions' => array(
     'reports_id' => $id 
    ) 
)); 
// merge this in with existing data 
$this->data = Set::merge($reports, $this->data); 

假設$ id在URL中作爲重定向的一部分。這就是你如何將這些數據分配給$ this-> data,所以你可以在你的表單中使用它。

而且在你的表格,你可以參考一下,像這樣多個字段:

$this->Form->create('Report'); 

foreach ($data['Report'] as $i => $report) { 
    $this->Form->input('Report.'.$i.'.id'); 
    $this->Form->input('Report.'.$i.'.user_id'); 
    $this->Form->input('Report.'.$i.'.reports_id'); 
    $this->Form->input('Report.'.$i.'.area_id'); 
    $this->Form->input('Report.'.$i.'.area'); 
    $this->Form->input('Report.'.$i.'.count'); 
    $this->Form->input('Report.'.$i.'.comment'); 
} 

$this->Form->end('Submit); 

你可以read more about forms for multiple records at the CakePHP manual

+0

這個數組是不是從一個表單輸入也沒有爲它的視圖。它是隱藏的。我不希望人們看到它。我需要它在後臺運行,自動填充或預先填充數據庫表,然後將用戶重定向到可以編輯此數據的視圖。它包含一些需要保存或插入到數據庫的靜態和一些動態分配的值。目前,這是在我的控制器中。這個數組將如何分配給$ data? – sloga

1

這是爲我做

$count = 27; 
$v = 0; 
    do { 
     ######### save report rows to database 
     $this->Report->create(); 
     $this->Report->set(array('reports_id' => $id , 'area' => $v)); 
       $this->Report->save(); 
     $v++; 
    } while ($v < $count);