我正在尝试将一个名为"vendorping“的EAV属性添加到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??]
}
}发布于 2010-11-30 07:39:31
这是可行的:
// 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();发布于 2010-11-28 09:11:58
向EAV模型添加实体需要的不仅仅是向eav_entity_type表添加一行。EAV Setup Resources (运行安装程序脚本的类)有一个installEntities方法为您处理此问题。最好把整件事当作一个黑盒来处理,除非你真的想追踪正在发生的一切。在EAV系统周围随机添加数据和表,直到某些东西正常工作,几乎总是会导致系统以某种微妙的方式崩溃。这类似于直接摆弄RAM中的内存值。
My article on EAV模型应该涵盖您需要了解的内容。如果在那之后你仍然有问题,带着具体的问题回来。
https://stackoverflow.com/questions/4294494
复制相似问题