CakePHP 2 beforeSave()无效

时间:2012-01-19 13:26:10

标签: php cakephp before-save

我必须在保存之前操纵发布的数据。 $ this-> data是一个像这样的数组

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->数据),结果是一个空数组,我什么都不做。

1 个答案:

答案 0 :(得分:1)

beforeSave方法应该只返回truefalse来告诉save函数它是否可以继续,所以你只能把逻辑放在那里,但不会影响数据本身。

要修改数据,在调用保存功能之前,只需(重新)设置控制器功能,例如:

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);