1

im having some problems with saving data to the database. i've modified a brands extension to have more than one image but when i try to save the image src into the database it doesnt save. i've copied what they have already provided so that more images can be added. the thing is that the image saves to the new folder location but just does not add to the database.

i've cleared out the cache using 'flush magento cache' and 'flush cache storage' as other have suggested but it still does not work.

below is the code to save the brand and the image.(base_image is the one that was already provided and image_2 is my image that i want to add)

the brand controller code:

class Netsol_Brand_Adminhtml_BrandController extends Mage_Adminhtml_Controller_Action {

protected function _initAction()
{
    $this->loadLayout()
        ->_setActiveMenu('brand/items')
        ->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Brands Manager'));
    return $this;
}   

public function indexAction() {
    $this->_initAction();
    $this->_addContent($this->getLayout()->createBlock('brand/adminhtml_brand'));
    $this->renderLayout();
}

public function editAction()
{
    $brandId     = $this->getRequest()->getParam('id');
    $brandModel  = Mage::getModel('brand/brand')->load($brandId);
    if ($delete = $this->getRequest()->getParam('delete')){
        switch ($delete){
            case 'logo':
            case 'image_2':
            case 'base_image':
                $params = $this->getRequest()->getParams();
                unset($params['delete']);
                if($brandModel->getFeatured() && 'image_2' == $delete){
                    Mage::getSingleton('adminhtml/session')->addError(Mage::helper('brand')->__('Image must be uploaded for Featured Brand'));
                    Mage::getSingleton('adminhtml/session')->setFormData($brandModel->getData());
                    $this->_redirect('*/*/*/', $params);
                    return;
                }
                if ($brandModel->getFeatured() && 'base_image' == $delete){
                    Mage::getSingleton('adminhtml/session')->addError(Mage::helper('brand')->__('Image must be uploaded for Featured Brand'));
                    Mage::getSingleton('adminhtml/session')->setFormData($brandModel->getData());
                    $this->_redirect('*/*/*/', $params);
                    return;
                }
                $filename = $brandModel->getData($delete);
                $path = Mage::getBaseDir('media') . DS . 'brand' . DS . ($delete == 'logo'?'logo'.DS:'');
                if (file_exists($path.$filename)){
                    unlink($path.$filename);
                }
                $brandModel->setData($delete, '');
                $brandModel->save();
                $this->_redirect('*/*/*/', $params);
                return;


        }
    }
    if ($brandModel->getId() || $brandId == 0) {

        Mage::register('brand_data', $brandModel);

        $this->loadLayout();
        $this->_setActiveMenu('brand/items');

        $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Brands'));
        $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));

        $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);

        $this->_addContent($this->getLayout()->createBlock('brand/adminhtml_brand_edit'))
             ->_addLeft($this->getLayout()->createBlock('brand/adminhtml_brand_edit_tabs'));

        $this->renderLayout();
    } else {
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('brand')->__('Item does not exist'));
        $this->_redirect('*/*/');
    }
}

    public function newAction()
    {
        $this->_forward('edit');
    }

    public function saveAction(){
        if ( $this->getRequest()->getPost() ) {
            try {
                $postData = $this->getRequest()->getPost();
                $brandModel = Mage::getModel('brand/brand');
               if (isset($_FILES['base_image']['name']) && $_FILES['base_image']['name'] != '') {
                    try {
                        /* Starting upload */   
                        $uploader = new Varien_File_Uploader('base_image');
                        // Any extention would work
                        $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                        $uploader->setAllowRenameFiles(false);
                        $uploader->setFilesDispersion(false);
                        // We set media as the upload dir
                        $path = Mage::getBaseDir('media') . DS . 'brand' . DS;
                        //$uploader->save($path, $_FILES['image']['name'] );
                        $imagename = md5($_FILES['base_image']['name'].time()) . '.' . substr(strrchr($_FILES['base_image']['name'], '.'), 1);
                        $uploader->save($path, $imagename);
                    } catch (Exception $e) {
                        echo $e->getMessage();
                    }
                if (isset($imagename)){
                    //this way the name is saved in DB
                    $postData['base_image'] = $imagename;
                }
            }
            if (isset($_FILES['image_2']['name']) && $_FILES['image_2']['name'] != '') {
                try {
                    /* Starting upload */   
                    $uploader = new Varien_File_Uploader('image_2');
                    // Any extention would work
                    $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                    $uploader->setAllowRenameFiles(false);
                    $uploader->setFilesDispersion(false);
                    // We set media as the upload dir
                    $path = Mage::getBaseDir('media') . DS . 'brand' . DS . 'image2' . DS;
                    //$uploader->save($path, $_FILES['image']['name'] );
                    $image2name = md5($_FILES['image_2']['name'].time()) . '.' . substr(strrchr($_FILES['image_2']['name'], '.'), 1);
                    $uploader->save($path, $image2name);
                } catch (Exception $e) {
                }
                if (isset($image2name)){
                    //this way the name is saved in DB
                    $postData['image_2'] = $image2name;

                }
            }

            if (isset($_FILES['logo']['name']) && $_FILES['logo']['name'] != '') {
                try {   
                    $uploader = new Varien_File_Uploader('logo');
                    $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                    $uploader->setAllowRenameFiles(false);
                    $uploader->setFilesDispersion(false);
                    $path = Mage::getBaseDir('media') . DS . 'brand' . DS . 'logo' . DS;
                    $logoName = md5($_FILES['logo']['name'].time()) . '.' . substr(strrchr($_FILES['logo']['name'], '.'), 1);
                    $uploader->save($path, $logoName);
                } catch (Exception $e) {
                }
                if (isset($logoName)){
                    //this way the name is saved in DB
                    $postData['logo'] = $logoName;
                }
            }
            //Save attribute if new brand is added
            if($this->getRequest()->getParam('id') == ''){
                  $arg_attribute = 'brand_name';
                  $arg_value = $postData['name'];

                  $attr_model = Mage::getModel('catalog/resource_eav_attribute');
                  $attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
                  $attr_id = $attr->getAttributeId();

                  $option['attribute_id'] = $attr_id;
                  $option['value']['any_option_name'][0] = $arg_value;

                  $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
                //Get the last attribute id and get the option id.
                  $setup->addAttributeOption($option);
                  $db_write = Mage::getSingleton('core/resource')->getConnection('core_read');
                  $lastId = $db_write ->lastInsertId();
                  $select = $db_write->select()->from('eav_attribute_option_value', array('option_id'))->where('value_id=?',$lastId);
                  $rowArray =$db_write->fetchRow($select);
                  $postData['option_id'] =  $rowArray['option_id'];
            }

            $brandModel->setId($this->getRequest()->getParam('id'))
                ->setName($postData['name'])
                ->setDescription($postData['description'])
                ->setStatus($postData['status'])
                ->setIsFeature($postData['is_feature'])
                ->setBaseImage($postData['base_image'])
                ->setOptionId($postData['option_id'])
                ->setLogo($postData['logo'])
                ->setImage2($postData['image_2'])
                ->save();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Brand was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setBrandData(false);

                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setBrandData($this->getRequest()->getPost());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }
        $this->_redirect('*/*/');
    }

    public function deleteAction(){
        if( $this->getRequest()->getParam('id') > 0 ) {
            try {
                $brandModel = Mage::getModel('brand/brand');

                /*Load brand with the help of brand id and delete the option too with respect to the attribute*/
                $brandId = $this->getRequest()->getParam('id');
                $brand = Mage::getModel('brand/brand')->load($brandId);
                $arg_attribute = "brand_name";
                $option_id = $brand->getOptionId();//Getting the option id(attribute option)
                $attr_model = Mage::getModel('catalog/resource_eav_attribute');
                $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product',$arg_attribute);
                $options = $attribute->getSource()->getAllOptions();
                $options['delete'][$option_id] = true; 
                $options['value'][$option_id] = true;

                $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
                //Removing the option id.
                $setup->addAttributeOption($options);

                $brandModel->setId($this->getRequest()->getParam('id'))
                    ->delete();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Brand was successfully deleted'));
                $this->_redirect('*/*/');
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            }
        }
        $this->_redirect('*/*/');
    }

    /**
    * Product grid for AJAX request.
    * Sort and filter result for example.
    */
    public function gridAction(){
        $this->loadLayout();
        $this->getResponse()->setBody($this->getLayout()->createBlock('brand/adminhtml_brand_grid')->toHtml());
    }

    public function massStatusAction(){
        $this->_initAction();
        $brandIds = $this->getRequest()->getParam('brand');
        if(!is_array($brandIds)) {
            Mage::getSingleton('adminhtml/session')->addError($this->__('Please select Brands Page(s)'));
        } else {
            try {
                foreach ($brandIds as $brandId) {
                    $brand = Mage::getSingleton('brand/brand')
                        ->load($brandId)
                        ->setStatus($this->getRequest()->getParam('status'))
                        ->setIsMassupdate(true)
                        ->save();
                }
                $this->_getSession()->addSuccess(
                    $this->__('Total of %d record(s) were successfully updated', count($brandIds)));
            } catch (Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/index');
    }

    public function massDeleteAction(){
        $this->_initAction();
        $brandIds = $this->getRequest()->getParam('brand');
        if(!is_array($brandIds)) {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select Brands Page(s)'));
        } else {
            try {
                foreach ($brandIds as $brandId) {
                $brand = Mage::getModel('brand/brand')->load($brandId);
                /*Delete the attribute option too*/
                $arg_attribute = "brand_name";
                $option_id = $brand->getOptionId();//Getting the option id(attribute option)
                $attr_model = Mage::getModel('catalog/resource_eav_attribute');
                $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product',$arg_attribute);
                $options = $attribute->getSource()->getAllOptions();
                $options['delete'][$option_id] = true; 
                $options['value'][$option_id] = true;

                $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
                //Removing the option id.
                $setup->addAttributeOption($options);
                $brand->delete();
            }
            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Total of %d record(s) were successfully deleted', count($brandIds)));
        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }
     }
    $this->_redirect('*/*/index');
 }
}

the brand form for adding the image_2:

$fieldset->addField('image_2', 'file', array(
        'label'      => Mage::helper('brand')->__('Image 2'),
        'required'   => false,
        'name'       => 'image_2',
        'after_element_html' => (''!=Mage::registry('brand_data')->getData('image_2')?'<p style="margin-top: 5px"><img src="'.Mage::getBaseUrl('media') . 'brand/' . Mage::registry('brand_data')->getData('image_2'). '" width="60px" height="60px" /><br/><a href="'.$this->getUrl('*/*/*/', array('_current'=>true, 'delete'=>'image_2')).'">'.Mage::helper('brand')->__('Delete Image 2').'</a></p>':''),
        ));

      $fieldset->addField('image_2_', 'hidden', array(
        'name'      => 'image_2_',
        ));

       Mage::registry('brand_data')->setData('image_2_', Mage::registry('brand_data')->getData('image_2'));

the database:

 CREATE TABLE IF NOT EXISTS {$this->getTable('brand')} (
  `brand_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'brand id',
  `option_id` int(11) NOT NULL COMMENT 'the brand id set from attribute',
  `name` varchar(255) NOT NULL DEFAULT '',
  `description` text NOT NULL,
  `is_feature` smallint(6) NOT NULL DEFAULT '0',
  `base_image` varchar(255) NOT NULL DEFAULT '',
  `logo` varchar(255) NOT NULL DEFAULT '',
  `status` smallint(6) NOT NULL DEFAULT '0' COMMENT 'Status',
  `created_time` timestamp NULL,
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `image_2` varchar(255) NOT NULL DEFAULT '',
  `image_3` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`brand_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8

the original brand extension is available HERE

Thanks in advance :)

  • Maybe it has something to do with the name of your field? Magento tries to convert camelcase to your field name, but your column has a _2. I'm thinking maybe ->setImage2(... is converted to image2 instead of image_2. Maybe try using ->setData('image_2', $postData['image_2']) to ensure the correct column name is used. You can also change your column names. – Vic Aug 25 '15 at 23:37
  • I added it as an answer. Please mark it if it helped you. – Vic Aug 26 '15 at 14:58
1

Change the name of your field.

Magento is parsing ->setImage2(... as image2 instead of image_2. Either change the name of your field or do this:

   ->setData('image_2', $postData['image_2'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.