2014-03-19 164 views
1

我正在爲我製作的網站製作管理工具,該管理工具允許管理員將圖像上載到服務器上的文件夾,該文件夾存儲圖庫的圖像。該文件正在正確上傳,但圖像名稱未放置在數據庫中。該名稱應放置在「路徑」字段的「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 
    ?> 
+0

如果你看看你最後一個關於cakePHP的問題,你會注意到模型名稱應該是'GalleryImage'。 – skywalker

+0

@Skywalker GalleryImage適用於GalleryImagesController,管理員模型僅適用於AdminsController,Admin僅適用於管理員操作。這是不正確的? – SunT

+0

'Admin'和'GalleryImage'模型之間有任何關係嗎?閱讀[Model Assocations](http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html) – skywalker

回答

1

解決

控制器

<?php 
    class GalleryController extends AppController { 

    public function admin_upload_image(){ 
     $this->layout = 'admin_index'; 
     if($this->request->is('post') || $this->request->is('put')) { 
      /* $file = $this->request->data['gallery_images']['path']['name'];*/ 
     $file = array(
       'GalleryImage' => array(
       'path' => $this->request->data['gallery_images']['path']['name'] 
             ) 
       ); 
      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']); 

      $this->loadModel('GalleryImage'); 
      $this->GalleryImage->create(); 

      if($this->GalleryImage->save($file)){ 
       $this->Session->setFlash(__('Ficheiro carregado com sucesso!')); 
      } 
      else{ 
       $this->Session->setFlash(__('Erro ao carregar o ficheiro!')); 
      } 
     } 
    } 
} 
?> 

查看

<h2>Adicionar Fotografia</h2> 
    <?php 
echo "<br>"; 
echo $this->Form->create('GalleryImage',array('type'=>'file')); 
echo $this->Form->file('gallery_images.path'); 
echo "<br>"; 
//echo $this->Form->submit(); 
echo $this->Form->end('Guardar'); 

    ?> 

型號

<?php 
App::uses('AppModel', 'Model'); 
class GalleryImage extends AppModel{ 
    public $displayField ='path'; 
    public $useTable = 'gallery_images'; 
} 
    ?> 
1

adminsdb表是:

  • 模式Admin,您的應用程序文件:../app/Model/ Admin .PHP,
  • 控制器在你的應用程序文件:../app/Controller/ Admins Controller.php這樣,
  • 動作/功能add_foto()在您的控制器AdminsController.php中。

gallery_imagesdb表是:

  • 模式GalleryImage,您的應用程序文件:../app/Model/ GalleryImage .PHP
  • 控制器在您的應用程序file:../app/Controller/ GalleryImages Controller.php

瞭解更多關於蛋糕的命名約定:http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html


1 - 如果你想將數據保存到gallery_images救一個表

,你必須爲GalleryImage創建形式,如:

VIEW: ../app/View/GalleryImages/ add_foto.ctp

echo $this->Form->create('GalleryImage', array('type' => 'file')); // << db table gallery_images   
      // ... 
     echo $this->Form->input('admin_id', array('value' => $admin_id)); // << id of db table admins 
      // ... 
     echo $this->Form->file('path'); // << your field of db table gallery_images 
      // ... 
    echo $this->Form->end('Guardar'); 

CONTROLLER:../app/Controller/ GalleryImagesController。PHP

public function add_foto() { 
    // ... 
    // debug($this->request->data); die(); // << You can see Your data 
    if($this->request->data){ 
     // ... 
     $this->GalleryImage->save($this->request->data); 
     // ... 
    }; 
    // ... 
} 


2.保存許多表

如果您想將數據保存到數據庫表adminsgallery_images在同一時間(一種形式)。你必須使用$this->YourModelName->saveAll($this->request->data), 閱讀更多:http://book.cakephp.org/2.0/en/models/saving-your-data.html

,你必須定義模型關係/鏈接型號:belongs_to的/的has_many第一,如:

MODEL:admin.php的:

var $hasMany = array(  
       'GalleryImage' => array(
        'dependent' => true 
       ), 
      ); 

MODEL:GalleryImage.php

var $belongsTo = array('Admin'); 


然後VIEW:... /管理員/ add_foto.ctp

echo $this->Form->create('Admin', array('type' => 'file')); // << db table gallery_images 
    // ... 
    echo $this->Form->input('Admin.id', array('value' => $admin_id)); // << id of db table admins 
    echo $this->Form->input('Admin.name'); 
    echo $this->Form->input('Admin.surname'); 
    // ... 
    echo $this->Form->input('GalleryImage.0.admin_id', array('value' => $admin_id)); // << id of db table admins 
    echo $this->Form->file('GalleryImage.0.path'); // << your field of db table gallery_images 
    // ... 
    echo $this->Form->input('GalleryImage.1.admin_id', array('value' => $admin_id)); // << id of db table admins 
    echo $this->Form->file('GalleryImage.1.path'); // << your field of db table gallery_images 
    // ... 
echo $this->Form->end('Guardar'); 

而且控制器:../Controller/ AdminsController.php

public function add_foto() { 
     // ...  
     if($this->request->data){ 
      // ... 
      $this->Admin->saveAll($this->request->data); 
      // ... 
     }; 
     // ... 
    } 

希望這個幫助。