在我的客户Magento模块中,我已经为订单添加了一个自定义属性(hearedfrom),该属性在订单对象中设置(get after set返回值)但它似乎没有保存在DB中
mymodule / etc / config.xml
<resources>
<hearedfrom_setup>
<setup>
<module>Brainworx_Hearedfrom</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</hearedfrom_setup>
</resources>
MySqlInstall
$installer = $this;
$installer->startSetup();
//get the entity type for orders
$sql = "SELECT entity_type_id FROM ".$this->getTable('eav_entity_type')." WHERE entity_type_code='order'";
Mage::Log("Hearedfrom: " .$sql);
$row = Mage::getSingleton('core/resource')->getConnection('core_read')->fetchRow($sql);
$attribute = array(
'type' => 'text',
'label' => 'Hearedfrom',
'visible' => true,
'required' => false,
'user_defined' => true,
'searchable' => true,
'filterable' => true,
'comparable' => false,
);
//add attribute for the sale/order
$installer->addAttribute($row['entity_type_id'], 'hearedfrom', $attribute);
$installer->endSetup();
我在新属性中保存值的观察者。
public function hookToOrderSaveEvent()
{
/**
* NOTE:
* Order has already been saved, now we simply add some stuff to it,
* that will be saved to database. We add the stuff to Order object property
* called "hearedfrom"
*/
$order = new Mage_Sales_Model_Order();
$incrementId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order->loadByIncrementId($incrementId);
//Fetch the data from select box and throw it here
$_hearedfrom_data = null;
$_hearedfrom_data = Mage::getSingleton('core/session')->getBrainworxHearedfrom();
//$this->_getRequest()->getPost(‘myCustomerOrderComment’, false);
Mage::Log("Order brought by: ".$_hearedfrom_data);
//Save fhc id to order obcject
$order->setData(self::ORDER_ATTRIBUTE_FHC_ID, $_hearedfrom_data);
Mage::Log("test: ".$order->getHearedfrom());
$order->save();
}
在我的日志中,我可以在$ order中设置之前和之后看到值。 数据有一个值的条目。 在DB中,我可以看到EAV_ATTRIBUTE中的一个链接到EAV_ENTITY_TYPE和EAV_ENTITY_ATTRIBUTE的东西。 但我希望保存的值在EAV_ENTITY_TEXT中,不包含任何记录。
重新下载已下订单后,它不包含客户属性。
我在这里缺少什么?
由于
答案 0 :(得分:2)
你能在sales_flat_order表中看到你的属性吗?
如果不是,我相信您的问题是使用目录设置而不是销售设置。请尝试使用 Mage_Sales_Model_Resource_Setup 安装程序类。改为添加如下属性:
$installer->getConnection()->addColumn($installer->getTable('sales/order'), 'hearedfrom', array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => true,
'default' => 0
));
同样清洁magento缓存。我相信一些SQL查询仍然被缓存,即使它们都被禁用了。将订单保存到数据库时,DESCRIBE SQL查询将确定在数据库中保存了哪些属性。 我最近遇到了一个问题,即描述查询没有包含我的新列,因此未保存该值。
答案 1 :(得分:0)
通过将我的处理程序挂钩到order_save_after_event而不是order_save_event来解决它