1

I have custom module with two database tables table 1 : Showroom table 2: Showroomdetails and two forms one for showroom and another for showroomdetails.

This model saves data in both tables but when i open for in edit mode i am not able to get saved data from table two in showroomdetails form

ShowroomController.php

class AdminModules_Showroom_Adminhtml_ShowroomController extends Mage_Adminhtml_Controller_action
{  
    public function editAction() {
        $id = $this->getRequest()->getParam('id');
        $model = Mage::getModel('showroom/showroom')->load($id);

        if ($model->getId() || $id == 0) {
            $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
            if (!empty($data)) {
                $model->setData($data);
            }

            Mage::register('showroom_data', $model);

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

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

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

            /* add wyswig editor */
            if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
                $this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
            } /* end */

            $this->_addContent($this->getLayout()->createBlock('showroom/adminhtml_showroom_edit'))
                ->_addLeft($this->getLayout()->createBlock('showroom/adminhtml_showroom_edit_tabs'));

            $this->renderLayout();
        } else {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('showroom')->__('Item does not exist'));
            $this->_redirect('*/*/');
        }
    }
1
  • try to create the method in helper/data folder and then access the method using Mage::Helper('modulename')->Mehtod().
    – Keyul Shah
    Dec 16, 2013 at 13:27

1 Answer 1

2

The code you posted, really has no relation to your issue. It is the Model code that would be of interest, since it would be your Mdel and collection resources that are responsible for pulling in the required data.

Not having seen that code, I can only surmise that you are not pulling in the second tables data, when the (main) model loads.

What I would do (off the top of my head):

  1. Create a model, and associated collection resources classes for both your tables (this would allow you to later access the second tables data directly, via a model, if required, and manipulate it directly)
  2. In your main model, in its _afterLoad method, add some code to instantiate the second model, pass it the required linking id, and load it. Then you can save that result into the model as either a property, or in the data array, and access it from your edit form.

Something like this in your 'parent' model

protected function _afterLoad() {
    $childModel = mage::getModel('mymodule/childmodel')->load($this->getId(),'YOUR LINK COLUMN');
    $this->setChildModel($childModel);
}

Alternatively, if you are only using one model, you can adjust the parent model's collection, at the resource level, and adjust the collection to link in your second table.

Thus in your Models Resource collection.php you'd have something like this in the _beforeLoad() method, which joins in your second table

$this->getSelect()->join(Mage::getConfig()->getTablePrefix().'table_name_for_join', 'main_table.your_table_field ='.Mage::getConfig()->getTablePrefix().'table_name_for_join.join_table_field',array('field_name_you_want_to_fetch_from_db'));

TIP: when joining tables, and things don't work, get the underlaying sql that magento generates, and run that directly using some mysql tool. Having a look at what the sql is, generally gives you an idea of what to adjust in your syntax to get to teh desired SQL)

Hope that helps

2
  • thanks for your ans. I can get data from second table using following method $this->getSelect()->join(Mage::getConfig()->getTablePrefix().'table_name_for_join', 'main_table.your_table_field ='.Mage::getConfig()->getTablePrefix().'table_name_for_join.join_table_field',array('field_name_you_want_to_fetch_from_db')); But how can i use this data in edit form.
    – Sayali
    Dec 17, 2013 at 5:38
  • The same way you use it for the main form. Teh fields of the second table will slot in as data values in the collection array. Have you confirmed that you are actually getting data back? (get the sql sent to the database, and run it manually, and check the result, see if your data is there in the result) you get the sql simply by doing a echo $collection->getSelect();
    – ProxiBlue
    Dec 17, 2013 at 9:04

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.