`copyFieldset` 将一个源属性复制到多个目标属性
-
13-12-2019 - |
题
我的任务是将引号转换为自定义数据类型,并使用 Mage_Core_Helper_Data::copyFieldset
一直工作得很好,直到我有两个目标属性必须设置为相同的源属性。我尝试了几种不同的方法
<other_thing_convert_quote>
<grand_total>
<to_varien_object>total</to_varien_object>
</grand_total>
<quote_currency_code>
<to_varien_object>currency</to_varien_object>
</quote_currency_code>
<quote_id>
<to_varien_object>refnum</to_varien_object>
<to_varien_object>txn_uuid</to_varien_object>
</quote_id>
</other_thing_convert_quote>
上面是一张 quote_id
节点,多个 to_varien_object
节点。
<other_thing_convert_quote>
<grand_total>
<to_varien_object>total</to_varien_object>
</grand_total>
<quote_currency_code>
<to_varien_object>currency</to_varien_object>
</quote_currency_code>
<quote_id>
<to_varien_object>refnum</to_varien_object>
</quote_id>
<quote_id>
<to_varien_object>uuid</to_varien_object>
</quote_id>
</other_thing_convert_quote>
以上是多个 quote_id
节点,每个节点有一个 to_varien_object
节点。
这些方法都没有成功应用 quote_id
二者皆是 refnum
和 txn_uuid
.
看看代码,我不确定我正在寻找的是否可能,但希望我能被证明是错误的!
谢谢
解决方案
另一种解决方案是使用当 复制字段集 函数被称为:
$eventName = sprintf('core_copy_fieldset_%s_%s', $fieldset, $aspect);
Mage::dispatchEvent($eventName, array(
'target' => $target,
'source' => $source,
'root' => $root
));
正如您所看到的,事件名称是动态的,因此在这种情况下,如果您调用 copyFieldset('other_thing_convert_quote', 'to_varien_object', ...) 事件名称将是 core_copy_fieldset_other_thing_convert_quote_to_varien_object 这就是您要放入模块的 config.xml 中的内容:
<events>
<core_copy_fieldset_other_thing_convert_quote_to_varien_object>
<observers>
<modulename>
<type>singleton</type>
<class>modulename/observer</class>
<method>coreCopyFieldsetOtherThingConvertQuoteToVarienObject</method>
</modulename>
</observers>
</core_copy_fieldset_other_thing_convert_quote_to_varien_object>
</events>
最后在观察者方法中,您可以这样做来设置您需要的值:
public function coreCopyFieldsetOtherThingConvertQuoteToVarienObject($observer) {
// your 'other_thing'
$src = $observer->getSource();
// your 'varien_object'
$target = $observer->getTarget();
$target->setData('refnum', $src->getQuoteId());
$target->setData('txn_uuid', $src->getQuoteId());
}
其他提示
人们似乎对这个问题没有太大的兴趣,但如果将来有人偶然发现这个问题,这是我能够确定的最佳解决方案。Magento 从源获取数据并使用以下命令在目标上设置数据 getDataUsingMethod
/setDataUsingMethod
它尝试根据节点名进行“驼峰式”方法调用。例如:
<my_module_convert_thing>
<property_a>
<to_another>*</to_another>
</property_a>
</my_module_convert_thing>
会尝试打电话 setPropertyA
和 getPropertyA
在两个物体上。如果不是那些委托给 getData
和 setData
通过 Varien_Object::__call
有一个定义的方法,灵活性几乎没有限制。如果需要从一个源设置两个属性,则以下 XML 字段集将起作用:
<my_module_convert_thing>
<property_a>
<to_another>properties_ab</to_another>
</property_a>
</my_module_convert_thing>
如果该方法 setPropertiesAb
定义在 another
类,该方法可以处理两者的设置 property_a
和 property_b
使用 property_a
价值来自 thing
.
如果有一个更好的解决方案,基本上不需要扩展需要这种自定义方法的每个类,那就太好了,但这是我解决问题的最佳尝试!