Magento:在 sales/order_shipment 模型上设置自定义属性
我正在尝试将名为“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??]
}
}
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??]
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将实体添加到 EAV 模型不仅仅需要向
eav_entity_type
表添加一行。 EAV 安装资源(运行安装程序脚本的类)有一个installEntities
方法来为您处理此问题。最好将整个事情视为黑匣子,除非您真的想追踪正在发生的一切。在 EAV 系统周围随机添加数据和表,直到某些功能正常运行,几乎总是会导致系统以某种微妙的方式出现故障。这类似于直接修改 RAM 中的内存值。我关于 EAV 模型的文章应该涵盖您需要了解的内容。如果此后您仍然遇到问题,请带着具体问题回来。
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 ainstallEntities
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: