[英]CakePHP: Error Saving Data
So I'm having troubles saving my data in CakePHP. 所以我在将数据保存在CakePHP中时遇到了麻烦。
If I upload an image (meaning ['PictureForm']['file']['name'] exists), everything works fine and the data is saved. 如果我上传图像(表示['PictureForm'] ['file'] ['name']存在,则一切正常,并且数据已保存。 However, if ['PictureForm']['file']['name'] is null, and ['PictureForm']['url'] exists, then the image is correctly saved on disk, but then $this->PictureForm->save($this->data) fails. 但是,如果['PictureForm'] ['file'] ['name']为空,并且[[PictureForm'] ['url']存在,则图像已正确保存在磁盘上,但是$ this-> PictureForm ->保存($ this->数据)失败。
Anyone see anything blatantly wrong with my code? 有人看到我的代码有任何公然错误吗?
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' => '/'));
}
}
If the save fails, in the else block, instead of redirecting try to view the validation errors: 如果保存失败,请在else块中,而不是重定向,请尝试查看验证错误:
if ($this->PictureForm->save($this->data)) {
// code
} else{
debug($this->PictureForm->validationErrors);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.