我可以在beforeSave()中删除行为中的数据行吗? CakePHP 2.x

时间:2014-04-23 09:31:21

标签: php cakephp

我已经form,该用户可以留下一些空字段。

当我有空字段时,我想取消设置所有数据集(数据行) - 它的结构如model.0.fieldmodel.1.field等。

我不想在模型或控制器中编写额外的代码 - 应该在behavior中完成。

我使用cake 2.x和saveAll() - 它意味着蛋糕将每行的整个数据集分开以保存。

我已尝试在beforeSave()beforeValidate()中执行此操作。 在未设置数据行之后,Cake尝试将空数据(空array())保存到db。

感谢您的帮助。

更新

我使用saveAll()来保存关联模型,debug()数据示例:

array(
    'ProfileFile' => array(
        (int) 10 => array(
            'id' => '111',
            'file' => '971_2.jpg',
            'profile_id' => '32',
            'title' => '',
            'order' => '1',
            'publicated' => '1',
            'delete' => '0'
        ),
        (int) 0 => array(
            'id' => '',
            'profile_id' => '32',
            'type' => 'file',
            'title' => '',
            'order' => '',
            'publicated' => '1',
            'file' => array(
                (int) 0 => array(
                    'name' => '',
                    'type' => '',
                    'tmp_name' => '',
                    'error' => (int) 4,
                    'size' => (int) 0
                )
            )
        )
    ),
    'Profile' => array(
        'id' => '32',
        'slug' => 'tomasz-laguna',
        'name' => 'Tomasz',
        'surname' => 'Ĺaguna',
        'surname_letter' => 'l',
        'instrument' => 'dfd',
        'favorite_instrument' => '',
        'country' => 'dfdf',
        'birth_place' => '',
        'short' => 'dfdfd',
        'content' => '',
        'about_music' => '',
        'website' => '',
        'gender' => '1',
        'dict_profile_id' => '1',
        'user_id' => '',
        'dict_moderate_status_id' => '3',
        'thumb' => '971.jpg',
        'public' => '1',
        'publicated' => '1',
        'opt_comment_display' => '1',
        'opt_comment_start' => '4',
        'opt_comment_reply' => '2',
        'opt_friends_display' => '1',
        'opt_gallery_display' => '1',
        'opt_music_display' => '1',
        'opt_video_display' => '1',
        'thumb_new' => array(
            'name' => '',
            'type' => '',
            'tmp_name' => '',
            'error' => (int) 4,
            'size' => (int) 0
        ),
        'modified' => '2014-04-23 14:20:14'
    )
)

我想在' ProfileFile'中取消设置数据。到:

array(
        'ProfileFile' => array(
            (int) 10 => array(
                'id' => '111',
                'file' => '971_2.jpg',
                'profile_id' => '32',
                'title' => '',
                'order' => '1',
                'publicated' => '1',
                'delete' => '0'
            ),
        ),
        'Profile' => array(
            'id' => '32',
            'slug' => 'tomasz-laguna',
            'name' => 'Tomasz',
            'surname' => 'Ĺaguna',
            'surname_letter' => 'l',
            'instrument' => 'dfd',
            'favorite_instrument' => '',
            'country' => 'dfdf',
            'birth_place' => '',
            'short' => 'dfdfd',
            'content' => '',
            'about_music' => '',
            'website' => '',
            'gender' => '1',
            'dict_profile_id' => '1',
            'user_id' => '',
            'dict_moderate_status_id' => '3',
            'thumb' => '971.jpg',
            'public' => '1',
            'publicated' => '1',
            'opt_comment_display' => '1',
            'opt_comment_start' => '4',
            'opt_comment_reply' => '2',
            'opt_friends_display' => '1',
            'opt_gallery_display' => '1',
            'opt_music_display' => '1',
            'opt_video_display' => '1',
            'modified' => '2014-04-23 14:20:14'
        )
    )

对于saveAll(),Cake触发beforeSave()3次,它看起来像(debug:model& data):

 \app\Model\Behavior\UploaderBehavior.php (line 311)

'Profile'

\app\Model\Behavior\UploaderBehavior.php (line 311)

array(
    'ProfileFile' => array(
        (int) 10 => array(
            'id' => '111',
            'file' => '971_2.jpg',
            'profile_id' => '32',
            'title' => '',
            'publicated' => '1',
            'delete' => '0'
        ),
        (int) 0 => array(
            'id' => '',
            'profile_id' => '32',
            'type' => 'file',
            'title' => '',
            'publicated' => '1',
            'file' => array(
                (int) 0 => array(
                    'name' => '',
                    'type' => '',
                    'tmp_name' => '',
                    'error' => (int) 4,
                    'size' => (int) 0
                )
            )
        )
    ),
    'Profile' => array(
        'id' => '32',
        'slug' => 'tomasz-laguna',
        'name' => 'Tomasz',
        'surname' => 'Łaguna',
        'surname_letter' => 'l',
        'instrument' => 'dfd',
        'favorite_instrument' => '',
        'country' => 'dfdf',
        'birth_place' => '',
        'short' => 'dfdfd',
        'content' => '',
        'about_music' => '',
        'website' => '',
        'gender' => '1',
        'dict_profile_id' => '1',
        'user_id' => '',
        'dict_moderate_status_id' => '3',
        'thumb' => '971.jpg',
        'public' => '1',
        'publicated' => '1',
        'opt_comment_display' => '1',
        'opt_comment_start' => '4',
        'opt_comment_reply' => '2',
        'opt_friends_display' => '1',
        'opt_gallery_display' => '1',
        'opt_music_display' => '1',
        'opt_video_display' => '1',
        'thumb_new' => array(
            'name' => '',
            'type' => '',
            'tmp_name' => '',
            'error' => (int) 4,
            'size' => (int) 0
        ),
        'modified' => '2014-04-23 14:36:38'
    )
)

\app\Model\Behavior\UploaderBehavior.php (line 311)

'ProfileFile'

\app\Model\Behavior\UploaderBehavior.php (line 311)

array(
    'ProfileFile' => array(
        'profile_id' => '32',
        'id' => '111',
        'file' => '971_2.jpg',
        'title' => '',
        'publicated' => '1',
        'delete' => '0',
        'modified' => '2014-04-23 14:36:38'
    )
)

\app\Model\Behavior\UploaderBehavior.php (line 311)

'ProfileFile'

\app\Model\Behavior\UploaderBehavior.php (line 311)

array(
    'ProfileFile' => array(
        'profile_id' => '32',
        'id' => '',
        'type' => 'file',
        'title' => '',
        'publicated' => '1',
        'file' => array(
            (int) 0 => array(
                'name' => '',
                'type' => '',
                'tmp_name' => '',
                'error' => (int) 4,
                'size' => (int) 0
            )
        ),
        'modified' => '2014-04-23 14:36:38',
        'created' => '2014-04-23 14:36:38'
    )
)

0 个答案:

没有答案