我正在爲我製作的網站製作管理工具,該管理工具允許管理員將圖像上載到服務器上的文件夾,該文件夾存儲圖庫的圖像。該文件正在正確上傳,但圖像名稱未放置在數據庫中。該名稱應放置在「路徑」字段的「gallery_images」表格中。這怎麼解決?使用CakePHP將數據名未保存到數據庫表
我使用CakePHP 2.4.4
控制器
<?php
class AdminsController extends AppController{
public $components = array('RequestHandler');
public function admin_index(){
if(!$this->Session->check('Admin')){
$this->Session->setFlash('Está a aceder a uma zona restrita. Por favor faça Login.');
$this->redirect(array(
'controller' => 'admins',
'action' => 'login'));
}
$this->layout='admin_index';
}
public function add_foto() {
if(!$this->Session->check('Admin')){
$this->Session->setFlash('Está a aceder a uma zona restrita. Por favor faça Login.');
$this->redirect(array(
'controller' => 'admins',
'action' => 'login'));
}
$this->layout='admin_index';
$file=$this->request->data['gallery_images']['path'];
if($this->request->is('post') || $this->request->is('put')){
$this->Admin->create();
$this->Admin->save($file);
move_uploaded_file($this->data['gallery_images']['path']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/html/PushUp_app/app/webroot/img/gallery/' . $this->data['gallery_images']['path']['name']);
if($this->Admin->save($this->request->data)){
$this->Session->setFlash(__('Ficheiro carregado com sucesso!'));
}
}
//$this->Admin->id = $id;
//$this->Post->save($data=array($this->data['Admins']['path']), $params=array('fieldList'=>'path'));
//$this->Post->saveField('path', $this->data['Admins']['path']);
/*if ($this->ModelName->save($this->request->data)) {
$this->Session->setFlash('Data Saved!');
}*/
//if($this->request->is('post')){
// $this->Admin->save($this->request->data);
//}
//}
}
}
?>
查看
<h2>Adicionar Fotografia</h2>
<?php
echo "<br>";
echo $this->Form->create('Admin',array('type'=>'file'));
echo $this->Form->file('gallery_images.path');
echo "<br>";
//echo $this->Form->submit();
echo $this->Form->end('Guardar');
//validação é feita no AdminsController
?>
如果你看看你最後一個關於cakePHP的問題,你會注意到模型名稱應該是'GalleryImage'。 – skywalker
@Skywalker GalleryImage適用於GalleryImagesController,管理員模型僅適用於AdminsController,Admin僅適用於管理員操作。這是不正確的? – SunT
'Admin'和'GalleryImage'模型之間有任何關係嗎?閱讀[Model Assocations](http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html) – skywalker