CakePHP:保存数据时出错

时间:2012-04-19 21:49:15

标签: php cakephp

所以我在CakePHP中保存数据时遇到了麻烦。

如果我上传图片(意味着['PictureForm'] ['file'] ['name']存在),一切正常并保存数据。但是,如果['PictureForm'] ['file'] ['name']为空,并且['PictureForm'] ['url']存在,则图像正确保存在磁盘上,但是$ this-> PictureForm-> save($ this-> data)失败。

有人发现我的代码有任何明显错误吗?

if (isset($this->data['PictureForm'])) {
                            ///////////////////////////////////////////////////////DEV
                            if(!$this->data['PictureForm']['file']['name']) {
                                    if (!$this->data['PictureForm']['url']) {
                                            $this->Session->setFlash('Error: no image URL or file given!');
                                            $this->redirect(array('action' => '/'));
                                    } else {
                                            $fileExtension = getExtension($this->data['PictureForm']['url']);
                                            $this->request->data['PictureForm']['filename'] = randFilename() . "." . $fileExtension;
                                            file_put_contents('files/picture/' . $this->data['PictureForm']['filename'], file_get_contents($this->data['PictureForm']['url']));
                                    }
                            } else { //file was uploaded
                                    $fileExtension = getExtension($this->data['PictureForm']['file']['name']);
                                    if (!$fileExtension) {
                                            $this->Session->setFlash('Error: no file extension!');
                                            $this->redirect(array('action' => '/'));
                                    }
                                    $this->request->data['PictureForm']['filename'] = randFilename() . "." . $fileExtension;
                                    move_uploaded_file($this->data['PictureForm']['file']['tmp_name'], "files/picture/" . $this->data['PictureForm']['filename']);
                            }
                            $this->request->data = Sanitize::clean($this->request->data, array('encode' => false));
                            if ($this->PictureForm->save($this->data)) {
                                    resizeUpload($this->data['PictureForm']['filename']);
                                    $this->Session->setFlash('Picture <a href="/spootur/media/p/' . $this->data['PictureForm']['filename'] . '">saved</a>');
                                    $this->redirect(array('action' => 'p/' . $this->data['PictureForm']['filename']));
                            } else {
                                    $this->Session->setFlash('Error: could not save to db' . $this->data['PictureForm']['filename']);
                                    $this->redirect(array('action' => '/'));
                            }
                    }

1 个答案:

答案 0 :(得分:1)

如果保存失败,则在else块中,而不是重定向尝试查看验证错误:

if ($this->PictureForm->save($this->data)) {
    // code
} else{
    debug($this->PictureForm->validationErrors);
}