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

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

Tags: magento导入csv csv文件脚本

分享到: