轻松实现织梦网站数据迁移到新站点
来源:SegmentFault
时间:2023-01-28 08:16:05 113浏览 收藏
来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习数据库相关编程知识。下面本篇文章就来带大家聊聊《轻松实现织梦网站数据迁移到新站点》,介绍一下MySQL、PHP、lavarel、织梦、cms,希望对大家的知识积累有所帮助,助力实战开发!
众所周知,织梦已经开始收费了,这对国内版权意识增强应该不算坏事,但想要免费使用又不想惹麻烦的站长们就有点麻烦了。
有不少朋友来问,我们 MyCms 支不支持织梦数据迁移,目前我们已经实现一键导入织梦的原文章和商品了,现在简要讲述一下实现过程。
一、连接数据库
要想实现数据的迁移导入,那么先要得到数据库信息,所以我们第一步就要实现填写数据库信息功能。

可以打开织梦网站的 data/common.inc.php 文件对照填写。
单次导入数据字段为执行一次导入多少量的数据,默认100条,这个可以依照自己的服务器来调整。
这一步仅仅是保存数据库信息,别无他用。
附上连接数据库代码
//$this->config 为保存的数据库信息
$dedeConnection = array_merge([
'driver' => 'mysql',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => $this->config['dede_prefix'],
], $this->config);
config(['database.connections.dedecms' => $dedeConnection]);
$this->connection = DB::connection('dedecms');二、导入分类/文章
1.导入文章分类,并明确上下级关系。
public function articleCategory()
{
if (!Storage::exists("dede_article_category")) {
//导入分类
$categories = $this->connection
->table('arctype')->get();
$catArray = $catParentArray = [];
foreach ($categories as $category) {
$cid = ArticleCategory::insert([
'pid' => 0,
'name' => $category->typename,
]);
$catArray[$category->id] = $cid;
$catParentArray[$cid] = $category->topid;
}
foreach ($catParentArray as $key => $value) {
if ($value > 0) {
$ac = ArticleCategory::find($key);
$ac->pid = $catArray[$value];
$ac->save();
}
}
Storage::put("dede_article_category", json_encode($catArray));
} else {
$catArray = json_decode(Storage::get("dede_article_category"), true);
}
return $catArray;
}2.要先明确要导入文章的那些信息,然后对应好自身系统的字段,开始导入。
附上 MyCms 导入的代码给大家参考。
public function article(): JsonResponse
{
$date = date('Y-m-d H:i:s');
//最后导入ID
$lastId = Storage::exists("dede_article_last_id") ? Storage::get("dede_article_last_id") : 0;
$articles = $this->connection
->table('archives')
->leftJoin('addonarticle', 'aid', '=', 'id')
->where([
['channel', '=', 1],
['id', '>', $lastId],
])->limit($this->config['batch_number'])->get();
$importLog = [];
$catArray = $this->articleCategory();
foreach ($articles as $article) {
$aid = Article::insert([
'category_id' => $catArray[$article->typeid],
'title' => $article->title,
'content' => $article->body,
'description' => $article->description,
'img' => $article->litpic,
'author' => $article->writer,
'view' => $article->click,
'created_at' => date('Y-m-d H:i:s', $article->senddate),
'updated_at' => date('Y-m-d H:i:s', $article->pubdate),
]);
if ($article->shorttitle) {
$meta = [
'article_id' => $aid,
'meta_key' => 'short_title',
'meta_value' => $article->shorttitle,
];
ArticleMeta::insert($meta);
}
$lastId = $article->id;
$tagIds = (new ArticleTag)->insertTags(explode(",", trim($article->keywords, ",")));
(new ArticleTagRel)->insertRel($aid, $tagIds);
//导入记录
$importLog[] = [
'type' => '文章',
'oid' => $article->id,
'mid' => $aid,
'title' => $article->title,
'created_at' => $date,
'updated_at' => $date,
];
}
Dedecms::insertAll($importLog);
//写入导入最后ID
Storage::put("dede_article_last_id", $lastId);
return $this->result(true);
}三、导入商品
导入商品也是一样的道理,就不多少,直接附上代码。
public function goods()
{
$date = date('Y-m-d H:i:s');
$lastId = Storage::exists("dede_goods_last_id") ? Storage::get("dede_goods_last_id") : 0;
$articles = $this->connection
->table('archives')
->leftJoin('addonshop', 'aid', '=', 'id')
->where([
['channel', '=', 6],
['id', '>', $lastId],
])->limit($this->config['batch_number'])->get();
$importLog = [];
$catArray = $this->goodsCategory();
foreach ($articles as $article) {
$aid = Goods::insert([
'category_id' => $catArray[$article->typeid],
'goods_name' => $article->title,
'content' => $article->body,
'description' => $article->description,
'goods_image' => $article->litpic,
'view' => $article->click,
'shop_price' => $article->trueprice ?: $article->price,
'market_price' => $article->price,
'created_at' => date('Y-m-d H:i:s', $article->senddate),
'updated_at' => date('Y-m-d H:i:s', $article->pubdate),
]);
if ($article->shorttitle) {
$meta = [
'goods_id' => $aid,
'meta_key' => 'short_title',
'meta_value' => $article->shorttitle,
];
GoodsMeta::insert($meta);
}
$lastId = $article->id;
$importLog[] = [
'type' => '商品',
'oid' => $article->id,
'mid' => $aid,
'title' => $article->title,
'created_at' => $date,
'updated_at' => $date,
];
}
Dedecms::insertAll($importLog);
Storage::put("dede_goods_last_id", $lastId);
return $this->result(true);
}最后导入成功,并记录下来。

理论要掌握,实操不能落!以上关于《轻松实现织梦网站数据迁移到新站点》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
499 收藏
-
244 收藏
-
235 收藏
-
157 收藏
-
101 收藏
最新阅读
更多>
-
117 收藏
-
411 收藏
-
420 收藏
-
264 收藏
-
266 收藏
-
392 收藏
-
333 收藏
-
234 收藏
-
448 收藏
-
416 收藏
-
225 收藏
-
145 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习