简体   繁体   English

数据名称未使用CakePHP表单保存到数据库

[英]Data name not being saved to db using CakePHP Form

I am making an admin tool, for the site I am making, that allows the admin to make image uploads to a folder at the server, that stores images for a gallery. 我正在为我正在制作的网站制作一个管理工具,该工具允许管理员将图像上传到服务器上的文件夹,该文件夹存储画廊的图像。 The file is being uploaded correctly but the image name isn't being placed on the database. 文件已正确上传,但图像名称未放置在数据库中。 The name should be placed at the table "gallery_images", on the "path" field. 该名称应放在“路径”字段的“ gallery_images”表中。 How can this be fixed? 如何解决?

I am using CakePHP 2.4.4 我正在使用CakePHP 2.4.4

Controller 调节器

    <?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);
            //}
        //}
    }
}
    ?>

View 视图

    <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
    ?>

admins db table is: admins 数据库表是:

  • model Admin , your app file: ../app/Model/ Admin .php, Admin 模型 ,您的应用程序文件:../app/Model/ Admin .php,
  • controller in your app file: ../app/Controller/ Admins Controller.php, 控制器在您的应用程序文件:../app/Controller/ Admins Controller.php这样,
  • action/function add_foto() in Your controller AdminsController.php. 控制器AdminsController.php中的action / function add_foto()

gallery_images db table is: gallery_images 数据库表是:

  • model GalleryImage , your app file: ../app/Model/ GalleryImage .php , 模型 GalleryImage ,您的应用程序文件: ../ app/ Model/ GalleryImage .php
  • controller in your app file: ../app/Controller/ GalleryImages Controller.php 控制器在您的应用程序文件:../app/Controller/ GalleryImages Controller.php这样

Read more about Cake naming conventions: http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html 阅读有关Cake命名约定的更多信息: http : //book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html


1. - SAVE ONE TABLE 1.-保存一张桌子

If You want to save data to gallery_images , You have to create form for GalleryImage , like: 如果要将数据保存到gallery_images ,则必须为GalleryImage创建表单,例如:

VIEW: ../app/View/GalleryImages/ add_foto.ctp 查看: ../ 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 控制器 :../ 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. SAVE MANY TABLES 2.保存许多表

If You want to save data to db table admins and gallery_images in the same time (one form) . 如果要同时(一种形式)将数据保存到db表adminsgallery_images You have to use $this->YourModelName->saveAll($this->request->data) , read more: http://book.cakephp.org/2.0/en/models/saving-your-data.html 您必须使用$this->YourModelName->saveAll($this->request->data) ,了解更多信息: http : //book.cakephp.org/2.0/en/models/saving-your-data.html

and You have to define model relations/link models: belongs_to/has_many first, like: 并且您必须定义模型关系/链接模型:首先是belongs_to / has_many,例如:

MODEL : Admin.php: 型号 :Admin.php:

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

MODEL : GalleryImage.php 型号 :GalleryImage.php

var $belongsTo = array('Admin');


Then VIEW : .../Admins/ add_foto.ctp 然后查看 :... / Admins / 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');

And CONTROLLER : ../Controller/ AdminsController.php 控制器 :../ Controller/ AdminsController.php

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

Hope this help. 希望对您有所帮助。

Solved 解决了

Controller 调节器

    <?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!'));
            }
        }
    }
}
?>

View 视图

    <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');

    ?>

Model 模型

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM