1

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. How can this be fixed?

I am using 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
    ?>
3
  • If you look at your last question regarding cakePHP, you will notice model name should be GalleryImage. Commented Mar 19, 2014 at 17:55
  • @Skywalker The GalleryImage is for the GalleryImagesController and the Admin model for AdminsController, Admin for admin actions only. Is this made incorrectly? Commented Mar 19, 2014 at 18:23
  • Is there any relation between Admin and GalleryImage models? Read on Model Assocations Commented Mar 19, 2014 at 19:01

2 Answers 2

1

admins db table is:

  • model Admin, your app file: ../app/Model/Admin.php,
  • controller in your app file: ../app/Controller/AdminsController.php,
  • action/function add_foto() in Your controller AdminsController.php.

gallery_images db table is:

  • model GalleryImage, your app file: ../app/Model/GalleryImage.php,
  • controller in your app file: ../app/Controller/GalleryImagesController.php

Read more about Cake naming conventions: http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html


1. - SAVE ONE TABLE

If You want to save data to gallery_images, You have to create form for GalleryImage, like:

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. SAVE MANY TABLES

If You want to save data to db table admins and gallery_images in the same time (one form). You have to use $this->YourModelName->saveAll($this->request->data), read more: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:

MODEL: Admin.php:

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

MODEL: GalleryImage.php

var $belongsTo = array('Admin');


Then VIEW: .../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

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

Hope this help.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.