好多天没有写文章了,最近有点事情。今天我在这篇文章里面我将讲述关于magento 1数据库的迁移到magento 2的操作步骤。 本篇文章更新于:2019年04月13日
大家都知道magento 1的代码结构和magento 2的代码结构完全不同的,直接升级是行不通的。magento官网有介绍的是利用迁移工具来进行迁移,在这里我将介绍使用代码进行迁移。
下面我将直接使用代码操作使用数据库的迁移。本例只迁移客户信息。
第一步:在目标的m1的代码结构里面创建个api接口,创建app/code/Sky8g/Hello/controllers/Api/CustomerController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php class Sky8g_Hello_Api_TestController extends Mage_Core_Controller_Front_Action { public function indexAction() { $customerCollection = Mage::getModel('customer/customer')->getCollection(); $from = $this->getRequest()->getParam('from'); $to = $this->getRequest()->getParam('to'); if(empty($to) || empty($from) ){ echo json_encode([]); return; } $result = $customerCollection->addFieldToFilter('created_at', array("from" => $from, "to" => $to ) ); $customer = Mage::getModel('customer/customer'); foreach ($result as $customer) { # code... $customerId = $customer->getId(); $data[] = $customer->load($customerId)->getData(); } echo json_encode($data); } } |
保存上传到m1的代码中。
第二步:在目标的m2的代码结构里面创建app/code/Sky8g/Hello/Controller/Index/Customer.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | <?php namespace Sky8g\Hello\Controller\Index; class Customer extends \Magento\Framework\App\Action\Action { protected $_pageFactory; const PASSWORD_HASH = 0; const PASSWORD_SALT = 1; protected $_storeManager; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $pageFactory, \Magento\Store\Model\StoreManagerInterface $storeManager) { $this->_pageFactory = $pageFactory; $this->_storeManager = $storeManager; return parent::__construct($context); } private function setcurlconfig() { $setheader =array( 'timeout' =>40, 'CURLOPT_USERAGENT'=>'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36', 'CURLOPT_REFERER'=>'https://www.sky8g.com/', 'header'=>'' ); return $setheader; } private function upgradeCustomerHash($hash) { if (isset($hash)) { $hashExploded = $this->explodePasswordHash($hash); if (strlen($hashExploded[self::PASSWORD_HASH]) == 32) { $hash = implode(':', [$hashExploded[self::PASSWORD_HASH], $hashExploded[self::PASSWORD_SALT], '0']); } elseif (strlen($hashExploded[self::PASSWORD_HASH]) == 64) { $hash = implode(':', [$hashExploded[self::PASSWORD_HASH], $hashExploded[self::PASSWORD_SALT], '1']); } } return $hash; } /** * @param string $passwordHash * @return array */ private function explodePasswordHash($passwordHash) { $explodedPassword = explode(':', $passwordHash, 2); $explodedPassword[self::PASSWORD_SALT] = isset($explodedPassword[self::PASSWORD_SALT]) ? $explodedPassword[self::PASSWORD_SALT] : '' ; return $explodedPassword; } public function doUpdata($from,$to){ $url = 'https://www.sky8g.com/en/sky8g/api_customer/'; $params = ['from'=>$from,'to'=>$to]; $curlAdapter = $this->_objectManager->get(\Magento\Framework\HTTP\Adapter\CurlFactory::class)->create(); $curlAdapter->setConfig($this->setcurlconfig()); $curlAdapter->write('POST', $url, '1.1', $this->setcurlconfig(), http_build_query($params, null, '&') ); $result = $curlAdapter->read(); $data =json_decode($result,true); if(empty($data)){ echo "没有设置日期"; $_SESSION["flag"] = true; return; } $customer = $this->_objectManager ->get(\Magento\Customer\Model\Customer::class); $i=0; $created_in = $this->_storeManager->getStore()->getName(); $store_id = $this->_storeManager->getStore()->getStoreId(); foreach ($data as $customerData) { //print M2 password hash $customerData['password_hash'] = $this->upgradeCustomerHash( $customerData['password_hash'] ); $customerData['created_in'] = $created_in; $customerData['store_id'] = $store_id; $customer->setData($customerData); try { $customer->save(); $i++; echo $i.'<br/>'; }catch (Exception $e) { echo $e->getMessage(); } } } public function execute() { ignore_user_abort();//关闭浏览器仍然执行 set_time_limit(0);//让程序一直执行下去 if( !isset($_SESSION["flag"]) ){ echo '<meta http-equiv="refresh" content="2;url=http://www.sky8g2.com/sky8g/index/customer" />'; } ob_flush(); //将数据从php的buffer中释放出来 flush(); //将释放出来的数据发送给浏览器 if (!isset($_SESSION["num"])){ $_SESSION["num"] = 1391212800; // strtotime('2014-02-01') $from= $_SESSION["num"]; }else { $_SESSION["num"] = $_SESSION["num"]+2592000; //30天 $from = $_SESSION["num"]; $to = $_SESSION["num"]+2592000; } if(!isset($to)){ $to = 1393804800; } $from = date('Y-m-d H:i:s',$from); $to = date('Y-m-d H:i:s',$to); $this->doUpdata($from,$to); } } |
保存上传到magento 2的代码中。
第三步:
1 2 | 访问下面的域名即可进行数据库客户信息的迁移 http://www.sky8g2.com/sky8g/index/customer |
如果有不懂的地方请留言。