当前位置:首页 > PHP教程 > php高级应用 > 列表

magento导入csv文件到数据库脚本

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-21 22:03:45 浏览: 评论:0 

magento导入csv文件到数据库方法我们会使用到一个插件了SplFileObject,利用SplFileObject可以实现大数据量导入了,当然我们自己写得也是可以的,但是不一定比这个要好呀.

这是magento脚本的另外一种写法,个人觉得写的还行,高手莫见笑,用SplFileObject来处理大数据的csv文件效率高,代码如下:

  1. <?php 
  2. require_once 'shell/abstract.php'
  3. class Faarao_Import_Customer extends Mage_Shell_Abstract 
  4. protected $_files = array(); 
  5. protected $_datas = array(); 
  6. public function __construct() { 
  7. parent::__construct(); 
  8. set_time_limit(0); 
  9. if($this->getArg('file')) { 
  10. $this->_files = array_merge
  11. $this->_files, 
  12. array_map
  13. 'trim'
  14. explode(','$this->getArg('file')) 
  15. ); 
  16. foreach ($this->_files as $key=>$file) { 
  17. $extension = self::get_extension($file); 
  18. if($extension != 'csv'){ 
  19. unset($this->_files[$key]); 
  20. if(emptyempty($this->_files)){ 
  21. die(self::usageHelp()); 
  22. // Shell script point of entry 
  23. public function run() { 
  24. self::getDataFromCsv(); 
  25. if(emptyempty($this->_datas)){ 
  26. die("not found data in csv ! \r\n"); 
  27. $emailBooks = array(); 
  28. foreach ($this->_datas as $name => $datas) { 
  29. echo "filename: {$name} =======================\r\n"
  30. $importNum = 0; 
  31. foreach ($datas as $key => $data) { 
  32. if(emptyempty(trim($data[4]))){ 
  33. continue
  34. $customerData['firstname'] = trim($data[0]); 
  35. $customerData['lastname'] = trim($data[1]); 
  36. $customerData['phone'] = trim($data[2]); 
  37. $customerData['mobile'] = trim($data[3]); 
  38. $customerData['email'] = trim($data[4]); 
  39. $customerData['company'] = trim($data[5]); 
  40. $customerData['billing_address'] = $data[6] . $data[7] . $data[8]; 
  41. $customerData['billing_postcode'] = sprintf("%05d",trim($data[9])); 
  42. $customerData['billing_city'] = trim($data[10]); 
  43. $customerData['billing_country'] = trim($data[11]); 
  44. $customerData['shipping_address'] = $data[12] . $data[13] .$data[14]; 
  45. if(emptyempty($data[15]) && is_numeric($data[16])){ 
  46. $customerData['shipping_postcode'] = sprintf("%05d",trim($data[16])); 
  47. $customerData['shipping_city'] = trim($data[17]); 
  48. $customerData['shipping_country'] = trim($data[18]); 
  49. }else
  50. $customerData['shipping_postcode'] = sprintf("%05d", trim($data[15]));; 
  51. $customerData['shipping_city'] = trim($data[16]); 
  52. $customerData['shipping_country'] = trim($data[17]); 
  53. // $customerData['email'] = 'zouhongzhao@126.com'; 
  54. $customerData['country'] = 'Finland'
  55. $customerData['password'] = self::randomkeys(10); 
  56.  
  57. &nbsp; 
  58.  
  59. echo "customer email {$customerData['email']} ...\r\n"
  60. print_r($customerData); 
  61. $customer = Mage::getModel('customer/customer'); 
  62. $customer->setWebsiteId(Mage::app()->getWebsite()->getId()); 
  63. $customer->loadByEmail($customerData['email']); 
  64. if(!$customer->getId()) { 
  65. echo "insert ... \r\n"
  66. $customer->setEmail($customerData['email']); 
  67. $customer->setFirstname($customerData['firstname']); 
  68. $customer->setLastname($customerData['lastname']); 
  69. $customer->setPassword($customerData['password']); 
  70. }else
  71. echo "update ... \r\n"
  72. try { 
  73. $customer->save(); 
  74. $customer->setConfirmation(null); 
  75. $customer->save(); 
  76. //Make a "login" of new customer 
  77. Mage::getSingleton('customer/session')->loginById($customer->getId()); 
  78. $importNum ++; 
  79. $emailBooks[$customerData['email']] = array
  80. 'firstname'=>$customerData['firstname'], 
  81. 'lastname'=>$customerData['lastname'], 
  82. 'customer_mage_id'=>$customer->getId(), 
  83. 'passwd'=>$customerData['password'
  84. ); 
  85.  
  86. echo "customer save ok !\r\n"
  87. catch (Exception $ex) { 
  88. echo "customer save fail !\r\n"
  89. continue
  90.  
  91. if(trim($customerData['billing_address']) == trim($customerData['shipping_address']) 
  92. && $customerData['billing_postcode'] == $customerData['shipping_postcode'
  93. && $customerData['billing_city'] == $customerData['shipping_city']){ 
  94. $same_address = array ( 
  95. 'firstname' => $customerData['firstname'], 
  96. 'lastname' => $customerData['lastname'], 
  97. 'street' => $customerData['billing_address'], 
  98. 'company' => $customerData['company'], 
  99. 'city' => $customerData['billing_city'], 
  100. 'region_id' => ''
  101. 'region' => ''
  102. 'postcode' => $customerData['billing_postcode'], 
  103. 'country_id' => 'FI'
  104. 'telephone' => $customerData['phone'], 
  105. ); 
  106.  
  107. $customAddress = Mage::getModel('customer/address'); 
  108. //$customAddress = new Mage_Customer_Model_Address(); 
  109. $customAddress->setData($same_address
  110. ->setCustomerId($customer->getId()) 
  111. ->setIsDefaultBilling('1'
  112. ->setIsDefaultShipping('1'
  113. ->setSaveInAddressBook('1'); 
  114. try { 
  115. $customAddress->save(); 
  116. echo "sameAddress save ok !\r\n"
  117. catch (Exception $ex) { 
  118. echo "sameAddress save fail !\r\n"
  119. continue
  120. }else
  121. $billing_address = array ( 
  122. 'firstname' => $customerData['firstname'], 
  123. 'lastname' => $customerData['lastname'], 
  124. 'street' => $customerData['billing_address'], 
  125. 'company' => $customerData['company'], 
  126. 'city' => $customerData['billing_city'], 
  127. 'region_id' => ''
  128. 'region' => ''
  129. 'postcode' => $customerData['billing_postcode'], 
  130. 'country_id' => 'FI'
  131. 'telephone' => $customerData['phone'], 
  132.  
  133. ); 
  134. self::setBillingAddress($billing_address,$customer); 
  135. $shipping_address = array ( 
  136. 'firstname' => $customerData['firstname'], 
  137. 'lastname' => $customerData['lastname'], 
  138. 'street' => $customerData['shipping_address'], 
  139. 'company' => $customerData['company'], 
  140. 'city' => $customerData['shipping_city'], 
  141. 'region_id' => ''
  142. 'region' => ''
  143. 'postcode' => $customerData['shipping_postcode'], 
  144. 'country_id' => 'FI'
  145. 'telephone' => $customerData['phone'], 
  146. ); 
  147. self::setShippingAddress($shipping_address,$customer); 
  148. // die; 
  149. echo "import num: {$importNum} =======================\r\n"
  150. //save passwd 
  151. $fp = fopen('customer_record.log''w'); 
  152. fwrite($fp, json_encode($emailBooks)); 
  153. fclose($fp); 
  154.  
  155.  
  156. public function setBillingAddress($data,$customer){ 
  157. $customerAddress = Mage::getModel('customer/address'); 
  158.  
  159. if ($defaultShippingId = $customer->getDefaultBilling()){ 
  160. $customerAddress->load($defaultShippingId); 
  161. else { 
  162. $customerAddress 
  163. ->setCustomerId($customer->getId()) 
  164. ->setIsDefaultBilling('1'
  165. ->setSaveInAddressBook('1'
  166.  
  167. $customer->addAddress($customerAddress); 
  168.  
  169. try { 
  170. $customerAddress 
  171. ->addData($data
  172. ->save() 
  173. echo "BillingAddress save ok !\r\n"
  174. } catch(Exception $e){ 
  175. // Mage::log('Address Save Error::' . $e->getMessage()); 
  176. echo "BillingAddress save fail !\r\n"
  177.  
  178. public function setShippingAddress($data,$customer){ 
  179. $customerAddress = Mage::getModel('customer/address'); 
  180.  
  181. if ($defaultShippingId = $customer->getDefaultShipping()){ 
  182. $customerAddress->load($defaultShippingId); 
  183. else { 
  184. $customerAddress 
  185. ->setCustomerId($customer->getId()) 
  186. ->setIsDefaultShipping('1'
  187. ->setSaveInAddressBook('1'
  188.  
  189. $customer->addAddress($customerAddress); 
  190.  
  191. try { 
  192. $customerAddress 
  193. ->addData($data
  194. ->save() 
  195. echo "ShippingAddress save ok !\r\n"
  196. } catch(Exception $e){ 
  197. // Mage::log('Address Save Error::' . $e->getMessage()); 
  198. echo "ShippingAddress save fail !\r\n"
  199.  
  200. public function randomkeys($length) { 
  201. $returnStr=''
  202. $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ'
  203. for($i = 0; $i < $length$i ++) { 
  204. $returnStr .= $pattern {mt_rand ( 0, 61 )}; 
  205. return $returnStr
  206. public function getDataFromCsv(){ 
  207. $this->_files = array_unique($this->_files); 
  208. $csvCustomers = array(); 
  209. foreach ($this->_files as $filename) { 
  210. echo "current file: {$filename}\r\n"
  211. setlocale(LC_ALL, 'en_US.UTF-8'); 
  212. $content = file_get_contents($filename); 
  213. $data = mb_detect_encoding()($content'UTF-8', true); 
  214. // $data = iconv("CP1257","UTF-8", $content); 
  215. file_put_contents($filename$content); 
  216.  
  217. // fclose($handle); 
  218. // print_r($content);die; 
  219. $basename = basename($filename,".csv"); 
  220. $data = array(); 
  221. $tmp = array(); 
  222. $spl_object = new SplFileObject($filename'rb'); 
  223. $spl_object->seek(filesize($filename)); 
  224. $start = 0; 
  225. $num = $spl_object->key(); 
  226. $spl_object->seek($start); 
  227. while ($num-- && !$spl_object->eof()) { 
  228. $data[] = $spl_object->fgetcsv(); 
  229. $spl_object->next(); 
  230. foreach ($data as $key => $values) { 
  231. if($key == 0){ 
  232. continue
  233. $mergeValue = explode(';',implode(';'$values)); 
  234. // if(count($mergeValue) != 20){ 
  235. // continue; 
  236. // } 
  237. array_push($tmp,$mergeValue); 
  238.  
  239. $this->_datas[$basename] = $tmp
  240. return $this
  241.  
  242. public function get_extension($filename){ 
  243. return pathinfo($filename,PATHINFO_EXTENSION); 
  244. }//开源软件:phpfensi.com 
  245. // Usage instructions 
  246. public function usageHelp() 
  247. return <<<USAGE 
  248. Usage: php tetuan_customer_import.php --file a.csv,b.csv 
  249. \n 
  250. USAGE; 
  251. // Instantiate 
  252. $shell = new Faarao_Import_Customer(); 
  253. // Initiate script 
  254. $shell->run(); 

补充:有一些朋友使用的是excel文档,这样这个程序就不可以使用了,我们可以使用phpexcel插件来读取excel文件并写入到数据库中去.

Tags: magento导入csv csv文件脚本

分享到: