6

Currently, we have several forms that allow to edit/delete information, each one loading the data from one particular table. But what I want is to be able to load on the Form.php information from different tables.

On the code below there is a field name imagefilename. This field belongs to another table that is linked with a foreign key to the one I'm loading on the first if sentence on the line $data = Mage:: registry('banners_data')->getdata(); that was saved on the previous page at user click.

So I have the main ID and can search on the images table the needed data. But no matter how I try, it does not correctly load the information I need. I tried searching for samples but no luck or maybe I'm just not using the correct terms.

Here is a part of my _prepareForm function, without any code to load the image table as it was triggering some errors.

    protected function _prepareForm() {
        if (Mage::registry('banners_data')->getdata())  {
            $data = Mage::registry('banners_data')->getdata();
        }
        elseif (Mage::getSingleton('adminhtml/session')) {
            $data = Mage::getSingleton('adminhtml/session')->getdata();
            Mage::getSingleton('adminhtml/session')->getdata(null);
        }
        else {
            $data = array();
        }

        $form = new Varien_Data_Form(
            array('id' => 'edit_form',
                    'action' => $this->getUrl('*/*/save', $params),
                    'method' => 'post',
                    'enctype' => 'multipart/form-data',
    ));

        $fieldset = $form->addFieldset('banners_form', 
            array('legend' =>Mage::helper('banners')->__('Campaign details page')));

        $fieldset->addField('banner_name', 'text',
            array(  'label'     => Mage::helper('banners')->__('Name'),
                    'class'     => 'required-entry',
                    'required'  => true,
                    'name'      => 'banner_name',
                    'note'      => Mage::helper('banners')->__('campaign name'),
                    'tabindex' => 1
    ));
.....
        $fieldset->addField('imagefilename', 'file',
            array(  'label'   => Mage::helper('banners')->__('Image to upload'),
                    'name'    => 'imagefilename',
                    'disabled'=> false,
                    'note'    => Mage::helper('banners')->__('Image you want to upload'),
                    'readonly'=> true,
                    'tabindex'=> 10
.....
}
3
  • Can you show us the code where you fill the fields? (Typically $form->setValues()) Commented Jan 24, 2013 at 16:08
  • What you save in Mage::registry() is only available while the current page is loaded. You can't access the registry from the 'previous page'. You could try instead saving it to the user session. Commented Jan 24, 2013 at 16:34
  • @RickKuipers, I will, back in the office tomorrow morning. Fooman, I'm currently saving the data on user click on the page where I show the grid with all the rows. And all the fields on the form.php are filled without problems on the _prepareForm function I posted on the question.
    – Yaroslav
    Commented Jan 24, 2013 at 19:40

1 Answer 1

2

If you are certain, that you have imagefilename ID in $data, then load image file name from DB, and add it to $data with 'imagefilename' then when you do setValues, it should be picked up. If it is not too much overhead, add a JOIN to initial data loading part and fetch image file name there. Will save you one more trip to database.

2
  • That is what I was thinking off. But then the collection where I save all the values should be different. And therefore the line Mage::registry('banners_data')->getdata(); should change, I must use a different collection. Will give it a try.
    – Yaroslav
    Commented Jan 24, 2013 at 19:42
  • As far as I know when you save, model resource saves only valid data. I mean I have some models where I need to have some additional things, like you do, and saving collection did not produce errors.See Mage_Core_Model_Resource_Abstract::_prepareDataForTable Commented Jan 24, 2013 at 20:09

Your Answer

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

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