开发者

Magento: Setting a custom attribute on the sales/order_shipment model

I'm trying to add an EAV attribute called "vendorping" to the sales/order_shipment model. To accomplish this, I created the following file in my module:

// app\code\local\Jb\Vendorping\sql\vendorping_setup\mysql4-install-0.1.0.php

$this->startSetup();

$sql = 'SELECT entity_type_id FROM `'.$this->getTable('eav_entity_type').'` WHERE ent开发者_JAVA技巧ity_type_code = \'shipment\'';
$row = Mage::getSingleton('core/resource')
    ->getConnection('core_read')
    ->fetchRow($sql);
$entityTypeId = $row['entity_type_id'];

$c = array(
    'entity_type_id'  => $entityTypeId,
    'attribute_code'  => 'vendorping',
    'backend_type'    => 'int',
    'frontend_input'  => 'text',
    'is_global'       => '1',
    'is_visible'      => '0',
    'is_required'     => '0',
    'is_user_defined' => '0',
    'frontend_label'  => 'Vendor Confirmed',
    );
$attribute = new Mage_Eav_Model_Entity_Attribute();
$attribute->loadByCode($c['entity_type_id'], $c['attribute_code'])
    ->setStoreId(0)
    ->addData($c)
    ->save();

$this->endSetup();

Now, this is working fine -- this attribute is successfully added:

mysql> mysql> SELECT * FROM eav_attribute WHERE attribute_code LIKE 'vendorping';
+--------------+----------------+----------------+-----------------+---------------+--------------+---------------+----------------+----------------+------------------+----------------+--------------+-------------+-----------------+---------------+-----------+------+
| attribute_id | entity_type_id | attribute_code | attribute_model | backend_model | backend_type | backend_table | frontend_model | frontend_input | frontend_label   | frontend_class | source_model | is_required | is_user_defined | default_value | is_unique | note |
+--------------+----------------+----------------+-----------------+---------------+--------------+---------------+----------------+----------------+------------------+----------------+--------------+-------------+-----------------+---------------+-----------+------+
|          127 |              8 | vendorping     | NULL            | NULL          | int          | NULL          | NULL           | text           | Vendor Confirmed | NULL           | NULL         |           0 |               0 | NULL          |         0 |      |
+--------------+----------------+----------------+-----------------+---------------+--------------+---------------+----------------+----------------+------------------+----------------+--------------+-------------+-----------------+---------------+-----------+------+
1 row in set (0.00 sec)

But if I run this controller action, I can't seem to successfully save the new attribute:

// app\code\local\Jb\Vendorping\controllers\IndexController.php ===

class Jb_Vendorping_IndexController extends Mage_Core_Controller_Front_Action 
{
    public function pingAction()
    {
        // Get shipment
        $shipmentId = 1; // Set manually for now
        $shipment = Mage::getModel('sales/order_shipment')->load($shipmentId);
        var_dump($shipment->getOrder()->getShippingDescription());
            // Outputs:
            // string(17) "Flat Rate - Fixed" [So the shipment exists]

        // Save "vendorping" field and save
        $shipment->setVendorping(1);
        $shipment->save();

        // Reload shipment from database
        $shipment = Mage::getModel('sales/order_shipment')->load($shipmentId);

        // Check "vendorping" field
        var_dump($shipment->getVendorping());
            // Outputs:
            // NULL [Why??]
    }
}


Adding an entity to an EAV model takes more than just adding a row to the eav_entity_type table. EAV Setup Resources (the classes that run the installer scripts) have a installEntities method that takes care of this for you. It's best to treat the entire thing as a black box unless you really want to trace out everything that's going on. Randomly adding data and tables around the EAV system until something works almost always leads to a system that's broken in some subtle way. It's similar to directly fiddling with memory values in RAM.

My article on EAV models should cover what you need to know. If you're still having problems after that, come back with specific questions.


This worked:

// ModuleNamespace/ModuleName/sql/vendorping_setup/mysql4-install-0.1.0.php

$this->startSetup();

if (version_compare(Mage::getVersion(), '1.4.1.0', '>=')) { // If sales data is stored flat
    $w = $this->_conn;
    $w->addColumn($this->getTable('sales_flat_shipment'), 'vendorping', 'int');
} else {
    $eav = new Mage_Eav_Model_Entity_Setup('sales_setup');
    $eav->addAttribute('shipment', 'vendorping', array('type' => 'int'));
}

$this->endSetup();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜