`copyfieldset`の1つのソースプロパティへの複数のターゲットプロパティへ
-
13-12-2019 - |
質問
疑問をカスタムデータ型に変換する課題は、同じソースプロパティに設定されていなければならない2つのターゲットプロパティがあるまで、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>
.
上記のP>は、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>
.
上記のp>は、それぞれ1つのquote_id
ノードを持つ複数のto_varien_object
ノードです。
これらのアプローチのどちらも、quote_id
とrefnum
の両方にtxn_uuid
を正常に適用しません。
コードを見て、私が探しているものが可能であるかどうかはよくわかりませんが、うまくいけば私は間違って証明されることができます!
ありがとう
解決
別の解決策は、 copyfieldset 関数が呼び出されたときにディスパッチされるイベントを使用することです:
$eventName = sprintf('core_copy_fieldset_%s_%s', $fieldset, $aspect);
Mage::dispatchEvent($eventName, array(
'target' => $target,
'source' => $source,
'root' => $root
));
.
イベント名は動的であるため、この場合、 copyfieldset( 'other_thing_convert_quotte'、 '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はソースからデータを取得し、NodeNameに基づいて「ラクダ化された」メソッド呼び出しを試みるgetDataUsingMethod
/ setDataUsingMethod
を使用してターゲット上のデータを設定します。例えば:
<my_module_convert_thing>
<property_a>
<to_another>*</to_another>
</property_a>
</my_module_convert_thing>
.
は、2つのオブジェクトのsetPropertyA
とgetPropertyA
を呼び出します。 getData
を介してsetData
とVarien_Object::__call
に委任される呼び出しの代わりに、定義されたメソッドがありますが、柔軟性にはほとんど制限がありません。 1つのソースから2つのプロパティを設定する必要がある場合は、次の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
の両方の設定を処理できます。
このカスタムアプローチが必要なすべてのクラスを必要としないより良い解決策があったとしても、これは問題を解決する際の私の最善の刺し合いです!