2012-01-19 122 views
1

我必須在保存之前操作數據。 $這 - >數據是這樣的CakePHP 2 beforeSave()不起作用

Array 
(
    [Presence] => Array 
     (
      [Month] => 02 
      [Year] => 2012 
      [0] => Array 
       (
        [day] => 1 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [1] => Array 
       (
        [day] => 2 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [2] => Array 
       (
        [day] => 3 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [3] => Array 
       (
        [day] => 4 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [4] => Array 
       (
        [note] => 
       ) 

      [5] => Array 
       (
        [note] => 
       ) 

      [6] => Array 
       (
        [day] => 7 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [7] => Array 
       (
        [day] => 8 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [8] => Array 
       (
        [day] => 9 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [9] => Array 
       (
        [day] => 10 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [10] => Array 
       (
        [day] => 11 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [11] => Array 
       (
        [note] => 
       ) 

      [12] => Array 
       (
        [note] => 
       ) 

      [13] => Array 
       (
        [day] => 14 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [14] => Array 
       (
        [day] => 15 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [15] => Array 
       (
        [day] => 16 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [16] => Array 
       (
        [day] => 17 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [17] => Array 
       (
        [day] => 18 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [18] => Array 
       (
        [note] => 
       ) 

      [19] => Array 
       (
        [note] => 
       ) 

      [20] => Array 
       (
        [day] => 21 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [21] => Array 
       (
        [day] => 22 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [22] => Array 
       (
        [day] => 23 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [23] => Array 
       (
        [day] => 24 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [24] => Array 
       (
        [day] => 25 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [25] => Array 
       (
        [note] => 
       ) 

      [26] => Array 
       (
        [note] => 
       ) 

      [27] => Array 
       (
        [day] => 28 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [28] => Array 
       (
        [day] => 29 
        [presenza] => 1 
        [time_in] => 09:00 
        [time_out] => 18:00 
        [pausa] => 01:00 
        [hours] => 08:00 
        [client_id] => 4 
        [note] => 
       ) 

      [user_id] => 3 
     ) 

) 

數組,我要修改的日期和插入每一個「子陣列」的USER_ID。

但是,當我打電話,只是爲了調試,在我的beforeSave()調試($ this-> data)結果是一個空數組,我什麼都不能做。

回答

1

beforeSave方法只應返回truefalse來告訴save函數是否可以繼續,所以您只能將邏輯放在那裏,但不能影響數據本身。

要修改數據,只是(重新)-set它在你的控制器函數調用保存功能,像以前一樣:

foreach($this->data['Presence'] as $key => $data) { 
    if(is_array($key)) { 
     // This should be a sub-array 
     $this->data['Presence'][$key]['day'] = 1; // Set your 'new' day here 
     $this->data['Presence'][$key]['user_id'] = 1337; // Set your user id here 
    } 
} 

// Then, afterwards, save the adjusted data array 
$this->Presence->save($this->data); 
+0

做這一切的附加控制器功能得到它的工作,但它是正確的根據MVC做到這一點? – simofox

+0

只是說[cakephp文檔](http://book.cakephp.org/2.0/en/models/callback-methods.html)說你可以(並且根據MVC你應該)在beforeSave中修改數據。 – LuisClemente

+0

@LuisClemente你是對的,如果你重新設置'$ this-> data'數組的任何鍵,它將被保存。 – Oldskool