1

EDITED To Show Progress and Provide More Information

I have a collection that I am building from a join query that displays perfectly in Grid and I now wish to have magento edit and save the data. I am aware I will need to write the updates separately, but will cross that when I get to it.

Additionally all tables are "flat" tables.

I am calling my collection on the form page using

$id = Mage::app()->getRequest()->getParam('id');
$model = Mage::getModel('folder1/folder2')->getCollection()->addFieldToFilter('main_table.id', array('eq' => $id))->allTables();
$form->setValues($model->getData());

allTables() is in Collection.php and looks something like this:

public function allTables() {
    $select = $this->getSelect();
    $select->joinLeft(
        'joined_table',
        'main_table.id = joined_table.id',
        array('joined_val')
    );
    return $this;
}

This displays in the grid view perfectly and var_dump($model->getData()) shows all the data I'm expecting to see. When I press edit and try and populate my form it only displays the items in main_table

For instance:

$fieldset->addField('id', 'text', array(
    'label'     => Mage::helper('csm')->__('id'),
    'name'      => 'id',
    'readonly'  => 'true'
));

Shows the text field populated with the id but

$fieldset->addField('joined_val', 'text', array(
    'label'     => Mage::helper('csm')->__('Joined Val'),
    'name'      => 'joined_val'
));

Doesn't populate the field. Using joined_table.joined_val doesn't seem to work.

Output of var_dump($model->getData())

array(1) {
  [0]=>
  array(24) {
    ["id"]=>
    string(4) "1604"
    //snip
    ["joined_val"]=>
    string(2) "Plain text result to be shown"
1

Based on the output showing a value it seems to me like joined_val is a select/dropdown type of attribute. So what you are getting is the actual value id rather than the proper text value of what your looking for.

You likely will need to give this an additional paramter of options to define the display value.

$manufacturer_items = Mage::getModel('eav/entity_attribute_option')->getCollection()->setStoreFilter()
            ->join('attribute','attribute.attribute_id=main_table.attribute_id', 'attribute_code');

        foreach ($manufacturer_items as $manufacturer_item) :
            if ($manufacturer_item->getAttributeCode() == 'manufacturer')
                $manufacturer_options[$manufacturer_item->getOptionId()] = $manufacturer_item->getValue();
        endforeach;

$fieldset->addField('joined_val', 'text', array(
    'label'     => Mage::helper('csm')->__('Joined Val'),
    'name'      => 'joined_val',
    'options'   => $manufacturer_options,
    'index'     => 'joined_val'
));

The above generates an $manufacturer_options array in the form of id => value and places it into the field you are trying to display. Obviously you will need to correct to get the correct attribute linked over.

Update to Original Answer

The addField has been updated, having reviwed the details added in comment against the answer, the code has been modified: - Remove options (since you are getting the text rather than value). - name is incorrect, this should be index (the column)

$fieldset->addField('joined_val', 'text', array(
    'label'     => Mage::helper('csm')->__('Joined Val'),
    'options'   => $manufacturer_options,
    'index'     => 'joined_val'
));
3
  • Sorry, was ambiguous, am updating the code now. The joined_val is infact the plain text response I wish to show. Additionally all the tables are flat.
    – Joshua
    Jun 18 '15 at 23:44
  • @Joshua I have updated my answer with additional comments based on that which I believe should resolve that problem for you. Jun 19 '15 at 7:01
  • Hi, I seem to have fixed the problem and have outlined it here: pastebin.com/fdiV74gh however I don't think it's particularly best practice as I'm tricking it into thinking it's a collection (in which my joined tables do work) by overwriting the data. Is this a correct way of doing what I'm wanting to do?
    – Joshua
    Jun 21 '15 at 23:23

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.