2010-11-27 112 views
1

我試圖在sales/order_shipment模型中添加名爲「vendorping」的EAV屬性。要做到這一點,我創建了模塊中的以下文件:Magento:在sales/order_shipment模型上設置一個自定義屬性

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

現在,這是工作的罰款 - 成功添加該屬性:

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) 

但是,如果我運行這個控制器的動作,我似乎無法成功地保存新的屬性:

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

回答

0

這工作:

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

添加一個實體的EAV模型不僅需要添加一行到eav_entity_type表的更多。 EAV設置資源(運行安裝程序腳本的類)有一個installEntities方法爲您處理此問題。除非你真的想跟蹤所有的東西這是怎麼回事,最好把整個東西當作黑匣子。在EAV系統周圍隨機添加數據和表格,直到某些工作幾乎總是導致系統以某種微妙的方式被破壞。它類似於直接在RAM中處理內存值。

My article on EAV模型應該涵蓋你需要知道的。如果您在此之後仍然有問題,請回到特定的問題。

+0

谷歌搜索'installEntities`後,我發現[這個資源(http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/installing_custom_attributes_with_your_module)。感謝您的幫助,並感謝您的博客。 – 2010-11-28 16:41:35