2

I created a new attribute for orders using the following:

$installer =  new Mage_Sales_Model_Mysql4_Setup('core_setup');
$installer->startSetup();

$installer->addAttribute('order', 'erp_associated_ids',  array(
    'user_defined'  => true,
    'type' => 'varchar',
    'backend' => 'mycompany_mymodule/adminhtml_system_config_backend_csv',
    'label' => 'Associated Order IDs',
    'input' => 'text',
    'source' => '',
    'visible' => true,
    'required' => false,
    'default' => '',
    'frontend' => '',
    'unique' => false,
    'note' => 'CSV of order IDs'
));

$installer->endSetup();

I then created a backend model:

app/code/local/Mycompany/Mymodule/Model/Adminhtml/System/Config/Backend/Csv.php

class Mycompany_Mymodule_Model_Adminhtml_System_Config_Backend_Csv extends Mage_Core_Model_Config_Data {

    protected function _afterLoad()
    {
        if (is_string($this->getValue())) {
            $value = $this->getValue();
            $this->setValue(empty($value) ? false : explode(',', $value));
        }
    }

    protected function _beforeSave()
    {
        if (is_array($this->getValue())) {
            $this->setValue(implode(',', $this->getValue()));
        }
    }
}

The idea being that I can do the following:

Mage::getModel('sales/order')
    ->load(25)
    ->setErpAssociatedIds(array('12345', '98765'))
    ->save();

And that the backend model would convert the array to a CSV string like '12345,98765'. (I want a CSV because it's easier to search using pure MySQL than a serialized array.)

However, when I try to save this attribute I just get Array in the database.

I would assume that this stems from attributes created for flat tables not having an entry in eav_attribute which would note which backend_model I want to use. However, I feel like I must be missing something. Do flat tables not use backend models at all? Am I going to have to extend Mage_Sales_Model_Order to pull this off?

2

You cannot add backend models to flat table columns.
For the entities that are mapped to flat tables (like the orders) you have to use some events to manipulate the data.
There is no way to make a connection between an attribute and a backend model because flat entities don't have attributes. They have fields.

You can use sales_order_save_before to turn the array into a string.

public function beforeOrderSave($observer) {
    $order = $observer->getEvent()->getOrder();
    $erpIds = $order->getErpAssociatedIds();
    if (is_array($erpIds)) {
         $order->setErpAssociatedIds(implode(',', $erpIds));
    }
    return $this;
}

and if you want to get your field value as an array when you load an order observe the event sales_order_load_after

public function afterOrderLoad($observer) {
    $order = $observer->getEvent()->getOrder();
    $erpIds = $order->getErpAssociatedIds();
    if (!is_array($erpIds)) {
         $order->setErpAssociatedIds(explode(',', $erpIds));
    }
    return $this;
}
1

It appears you have a few issues with your class.

  1. Parent: Your class should extend Mage_Eav_Model_Entity_Attribute_Backend_Abstract which is the abstract for all backend models,
  2. Function naming: Your functions should be public, should be called beforeSave and afterLoad and should have an object as a parameter,
  3. Function Code: Code should get the attribute code from the object, check to see if that object has this attribute set, then format the attribute,
  4. Magento Core: It appears that Magento may already have a backend model for this, have a look if Mage_Eav_Model_Entity_Attribute_Backend_Array works for your case, this is used for multiselect attributes,
  • David, that is all very useful information. However, I never even got to test this class because the flat table attribute seems to ignore any backend model. So I still have my main question: Is there a way to use a backend model with a flat table attribute? – Tyler V. Jul 28 '14 at 23:25
  • Ah ok I missed that part sorry. I will check it when I can – David Manners Jul 29 '14 at 5:20
  • I also did not see that this was for an order attribute and not a catalog or customer attribute. Sorry :) – David Manners Jul 29 '14 at 9:47

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.