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

本秂侑毒 提交于 2019-12-23 03:33:09

问题


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 entity_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??]
    }
}

回答1:


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.




回答2:


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();


来源:https://stackoverflow.com/questions/4294494/magento-setting-a-custom-attribute-on-the-sales-order-shipment-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!