2012-02-25 90 views
0

我有這個模型http://pastebin.com/tSaTvLxW,這是我的控制器http://pastebin.com/iWeyvF71,這是相關的視圖add.ctp http://pastebin.com/YWqwMtqx,但是當我添加一個新的信息記錄,即使教育,經驗是空的總是記錄創建在各自的表中,我不不知道是什麼原因導致這種行爲,能幫助我找到這個嗎?爲什麼這總是保存數據,即使這些字段是空的?

+0

Browser Autocompletion Maybe?我們需要更多細節。 – MichaelH 2012-02-25 04:05:26

+0

不,自動完成已關閉,並且這些字段的數據也爲空,您需要哪些詳細信息? – ReynierPM 2012-02-25 04:08:03

回答

0

當我運行它,我看到下面的這 - $>請求 - >數據:

Array 
(
    [Information] => Array 
     (
      [name] => Test 
      [lastname] => Name 
      [email] => [email protected] 
      [mobile_phone] => 1561561566 
      [home_phone] => 1521521522 
      [address] => 123 Any Street 
      [other_information] => Other information 
      [picture] => Array 
       (
        [name] => avatar.png 
        [type] => image/png 
        [tmp_name] => /tmp/php2Jvveh 
        [error] => 0 
        [size] => 89486 
       ) 

     ) 

    [Education] => Array 
     (
      [0] => Array 
       (
        [education_content] => 
       ) 

     ) 

    [Experience] => Array 
     (
      [0] => Array 
       (
        [experience_content] => 
       ) 
     ) 
) 

你會發現,教育陣列和陣列的經驗,而空,仍然在存在$this->request->data。因爲您正在使用$this->Information->saveAll($this->request->data),它會將一組空白數據保存到表格中。如果您不想保存空白數據,請檢查數組以查看它是否爲空。如果是,請在saveAll之前取消設置。你有你的表單設置的方式,它會是這樣的:

if (empty($this->request->data['Experience'][0]['experience_content'])) { 
    unset($this->request->data['Experience']); 
} 
if (empty($this->request->data['Education'][0]['education_content'])) { 
    unset($this->request->data['Education']); 
} 
$this->Information->create(); 
    if ($this->Information->saveAll($this->request->data)) { 
    ...snip...