Magento中数据拷贝一实现
Mage_Sales_Model_Quote::setCustomer方法,有这么一行代码
Mage::helper('core')->copyFieldset('customer_account', 'to_quote', $customer, $this);
看下实现Mage_Core_Helper_Da
ta::copyFieldset
先从配置中读取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
$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