2012-06-16 35 views
2

我有這個$ data數組:(建在一個外殼,而不是一種形式)

(這裏調試)

array(
(int) 0 => array(
    (int) 0 => 's511013t', 
    (int) 1 => 'id3422', 
    (int) 2 => '1' 
), 
(int) 1 => array(
    (int) 0 => 's511013t', 
    (int) 1 => 'id3637', 
    (int) 2 => '1' 
) 
) 

而且使用saveMany:

$this->Dir->saveMany($data, array('validate' => 'false', 'fieldList' => array('name','dir_dataname', 'project_id'))); 

保存失敗,沒有錯誤。

我不知道我的$ data數組是否格式良好,(我很困惑它是否應該有另一個級別)我從sql選擇等構建它。但它確實包含我需要保存的所有信息,單模型。

我從shell中運行了這一切,它的工作保存單個記錄每次提供的字段名稱:

// this works 
$this->Dir->save(array('name' => $data[0][0], 'project_id' => $data[0][2], 'dir_dataname' => $data[0][1])); 

已經閱讀保存數據,我很想用saveMany和由於我的自定義$數據格式的fieldList。 (我不想在我的$數據中插入字段名稱)。
(沒有sql_dump顯示,因爲從shell任務中獲取它非常麻煩)

我花了整整一個晚上試圖弄清楚,你能指出我在正確的方向嗎?

回答

2

恕我直言,每個數組中的鍵不是數據庫表中的有效字段。它們應該代表與您的表格字段相同的名稱。 當你建立從SQL數組,輸出應該看起來像這些 - 關聯數組:

array(
(int) 0 => array(
    (string) name => 's511013t', 
    (string) dir_dataname => 'id3422', 
    (string) project_id => '1' 
), 
(int) 1 => array(
    (string) name => 's511013t', 
    (string) dir_dataname => 'id3637', 
    (string) project_id => '1' 
) 
) 

Cake2.0 Docs

$this->Dir->saveMany($data); 
+0

謝謝你們;我要做的是迭代我的索引數組並構建關聯結構;會回來。 <?PHP的 //關聯數組例如 $喜愛=陣列( 'OS'=> '窗口', '論壇'=> '的phpBB', '人'=>'周杰倫 ); ?> –

+0

可疑的是,我使用了一個糟糕的$數據結構。 goggled如何將我的索引數組轉換爲關聯,然後保存:'foreach($ dataIndexed as $ num_linea => $ line_of_text){ $ data [$ num_linea] ['name'] = $ dataIndexed [$ num_linea] [1] ; $ data [$ num_linea] ['dir_dataname'] = $ dataIndexed [$ num_linea] [0]; $ data [$ num_linea] ['qca_interv'] = $ dataIndexed [$ num_linea] [2];' } 感謝您的指導。 ! –

+0

我按照以下方式循環使用索引數組。你有什麼建議可以改進它嗎? 'foreach($ dataIndexed as $ num_linea => $ line_of_text){data_indexed [$ num_linea] [1]; $ data [$ num_linea] ['name'] = $ dataIndexed [$ num_linea] [1]; $ data [$ num_linea] ['dir_dataname'] = $ dataIndexed [$ num_linea] [0]; $ data [$ num_linea] ['qca_interv'] = $ dataIndexed [$ num_linea] [2]; }' –

0

您可以通過

debug($this->Dir->getDataSource()->getLog()); 

看起來好像你正在使用fieldList得到不正確的日誌。 fieldList是要列入保存到數據庫列入白名單的字段列表,而不是像您正在使用的「映射」。

您需要在數組中爲每個記錄指定field => value對,而不是數字索引。我可能是錯的,但我從來沒有看到過,根據文檔,它不會那樣。

+0

謝謝trigran,我通過你的解決方案得到了SQL日誌。 –