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
.....
}
Best Answer
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.