Magento中数据拷贝一实现

本文解析了Magento中客户账户信息如何通过Mage_Core_Helper_Data::copyFieldset方法复制到报价对象。具体步骤包括从配置中读取字段集,然后根据源对象类型选择合适的方式获取值并设置到目标对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mage_Sales_Model_Quote::setCustomer方法,有这么一行代码

Mage::helper('core')->copyFieldset('customer_account', 'to_quote', $customer, $this);



看下实现Mage_Core_Helper_Da

ta::copyFieldset
  $fields = Mage::getConfig()->getFieldset($fieldset, $root);
if (!$fields) {
return false;
}

$sourceIsArray = is_array($source);
$targetIsArray = is_array($target);

$result = false;
foreach ($fields as $code=>$node) {
if (empty($node->$aspect)) {
continue;
}

if ($sourceIsArray) {
$value = isset($source[$code]) ? $source[$code] : null;
} else {
$value = $source->getDataUsingMethod($code);
}

$targetCode = (string)$node->$aspect;
$targetCode = $targetCode == '*' ? $code : $targetCode;

if ($targetIsArray) {
$target[$targetCode] = $value;
} else {
$target->setDataUsingMethod($targetCode, $value);
}

$result = true;
}

 


先从配置中读取global/fieldsets/相应的节点,每个节点的格式为key为源对象中的属性名,v为配置;以下为一个实例

<prefix>
<create>1</create>
<update>1</update>
<name>1</name>
<to_quote>customer_prefix</to_quote>
<to_order>customer_prefix</to_order>
</prefix>

从源对象或数组中拷贝,如果是数组,则用key,对象则用相应的get方法,如tax_class_id,则方法为getTaxClassId

转载于:https://www.cnblogs.com/szprg/p/4588968.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值