我已经form
,该用户可以留下一些空字段。
当我有空字段时,我想取消设置所有数据集(数据行) - 它的结构如model.0.field
,model.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'
)
)