views:

537

answers:

1

Hi,

I have set an input field of type “Image” in an admin form using the code below:

<?php
// Tab Form
// File: app/code/local/MyCompany/Mymodule/Block/Adminhtml/Items/Edit/Tab/Form.php

class MyCompany_Mymodule_Block_Adminhtml_Items_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
  protected function _prepareForm()
  {
      $form = new Varien_Data_Form();
      $this->setForm($form);
      $fieldset = $form->addFieldset('mymodule_form',        array('legend'=>Mage::helper('mymodule')->__('Item information')));     

      $fieldset->addField('photo', 'image', array(
          'label'     => Mage::helper('mymodule')->__('Photo'),
          'required'  => false,
          'name'      => 'photo',
      ));     

      if ( Mage::getSingleton('adminhtml/session')->getMymoduleData() )
      {
          $form->setValues(Mage::getSingleton('adminhtml/session')->getMymoduleData());
          Mage::getSingleton('adminhtml/session')->setMymoduleData(null);
      } elseif ( Mage::registry('mymodule_data') ) {
          $form->setValues(Mage::registry('mymodule_data')->getData());
      }
      return parent::_prepareForm();
  }
}

And then, inside the controller save the image using:

public function saveAction() 
{
 if($data = $this->getRequest()->getPost()) {              
     $model = Mage::getModel('mymodule/speakers');        
     $model->setData($data)->setId($this->getRequest()->getParam('id'));

     $model->setKeynote($this->getRequest()->getParam('keynote'));

     // Save photo
     if(isset($_FILES['photo']['name']) && $_FILES['photo']['name'] != '') {
         try {    
             $uploader = new Varien_File_Uploader('photo');

             $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
             $uploader->setAllowRenameFiles(false);
             $uploader->setFilesDispersion(false);

             // Set media as the upload dir
             $media_path  = Mage::getBaseDir('media') . DS;                

             // Upload the image
             $uploader->save($media_path, $_FILES['photo']['name']);

             $data['photo'] = $media_path . $_FILES['photo']['name'];

         } 
         catch (Exception $e) {
             print_r($e);
             die;
         }                            
     }
     else {       
         if(isset($data['photo']['delete']) && $data['photo']['delete'] == 1) {
             $data['photo'] = '';
         }
         else {
             unset($data['photo']);
         }
      }
     if(isset($data['photo'])) $model->setPhoto($data['photo']);    

     try {                
         $model->save();

         Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('mymodule')->__('Item was successfully saved'));
         Mage::getSingleton('adminhtml/session')->setFormData(false);

         if ($this->getRequest()->getParam('back')) {
             $this->_redirect('*/*/edit', array('id' => $model->getId()));
             return;
         }
         $this->_redirect('*/*/');
         return;
     } 
     catch (Exception $e) {
         Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
         Mage::getSingleton('adminhtml/session')->setFormData($data);
         $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
         return;
     }
 }

 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('mymodule')->__('Unable to find item to save'));
 $this->_redirect('*/*/');
}

Long story short: When I save the item (using Save or Save and Continue Edit) in backend it saves well one time. Then the next time it gives the next error:

Notice: Array to string conversion in /home/wwwadmin/public_html/aaa.bbb.ccc/public/lib/Zend/Db/Statement/Pdo.php on line 232

The next saves ok. The next: error. The next ok… You know what I mean…

I was looking some code to see how this input type is used. But nothing yet. Neither inside the magento code. This is the only thing I’ve found: http://www.magentocommerce.com/wiki/how_to/how_to_create_pdf_upload_in_backend_for_own_module

Any ideas?

Thanks

A: 

When this line is runs:
$model->setData($data)->setId($this->getRequest()->getParam('id'));
$model->_data['image'] will be set to array('image'=>'[YOUR path]')

you should call method setData() after all manipulations with data['image'];

Victor