Cakephp 1.3:如何迁移到我的文章表格 - 函数
我有一个使用另一个 php cms 系统的应用程序,我现在将其转移到 CakePHP 1.3。我现在需要将 tblnewsarticles
中的所有旧文章转移到我的新 CakePHP 表 articles
中。这些表具有不同的字段,但我已经能够将我需要的所有字段映射到我的新表。我在 ArticlesController 类
中创建了以下临时 migrate
函数来为我执行此操作,但无法将数据保存到我的表中。一切似乎都很好,包括我已经使用 debug($data)
和 debug($articles)
进行了测试,它显示了数据,但我无法保存任何内容。我看不出这有什么问题,所以感谢任何帮助...
//TEMPORARY FUNCTION THAT WILL BE DELETED/DEACTIVATED AFTER MIGRATION
function migrate(){
// FOR TESTING PURPOSES I SET LIMIT=1, BUT I HAVE OVER 4000 ARTICLES TO TRANSFER
$articles = $this->Article->query(
"SELECT articleID,subcategoryID,article_title,article_html,article_date,image_extension FROM tblnewsarticles LIMIT 1;"
);
foreach($articles as $article){
$data['Article']['id'] = $article['tblnewsarticles']['articleID'];
$data['Article']['published'] = 1;
$data['Article']['title'] = $article['tblnewsarticles']['article_title'];
$data['Article']['body'] = $article['tblnewsarticles']['article_html'];
$data['Article']['main_image'] = 'cat_'.$article['tblnewsarticles']['subcategoryID'].'/'.$article['tblnewsarticles']['articleID'].'.'.$article['tblnewsarticles']['image_extension'];
$data['Article']['category_id'] = $article['tblnewsarticles']['subcategoryID'];
$data['Article']['user_id'] = 2;
$data['Article']['created'] = $article['tblnewsarticles']['article_date'];
if (!empty($data)) {
$this->Article->create();
$this->Article->save($data);
$this->Session->setFlash('Migration processed successfully');
}else{
$this->Session->setFlash('Unsuccessful');
}
}
}
======== 这就是我所做的对我有用的 =============== =
function migrateArticle(){
$articles = $this->Article->query(
"SELECT articleID,subcategoryID,article_title,article_html,article_date,image_extension FROM tblnewscategories;"
);
foreach($articles as $article){
$categoryID = $article['tblnewsarticles']['subcategoryID'];
$articleID = $article['tblnewsarticles']['articleID'];
$articleTitle = $article['tblnewsarticles']['article_title'];
$articleSlug = str_replace(" ","-",strtolower($articleTitle));
$articleBody = $article['tblnewsarticles']['article_html'];
$articleImage = 'cat_'.$article['tblnewsarticles']['subcategoryID'].'/'.$article['tblnewsarticles']['articleID'].'.'.$article['tblnewsarticles']['image_extension'];
$articleDate = $article['tblnewsarticles']['article_date'];
$this->Article->query("INSERT INTO categories (id, published, title, slug, body, main_image, category_id, user_id, created) VALUES('$articleID', '1', '$articleTitle', '$articleSlug', '$articleBody', '$articleImage', '$categoryID', '2', '$articleDate');");
}
}
I have an application that used another php cms system, which I am now transferring to CakePHP 1.3. I've come to a point where I need to transfer all my old articles in tblnewsarticles
to my new CakePHP table articles
. The tables have different fields, but I have been able to map all the ones I need to my new table. I created the following temporary migrate
function in my ArticlesController class
to perform this operation for me, but cannot have the data to save into my table. Everything seems to be OK, including I have done tests with debug($data)
and debug($articles)
and it shows the data, but I cannot save anything. I cannot see anything wrong with this so any help is appreciated...
//TEMPORARY FUNCTION THAT WILL BE DELETED/DEACTIVATED AFTER MIGRATION
function migrate(){
// FOR TESTING PURPOSES I SET LIMIT=1, BUT I HAVE OVER 4000 ARTICLES TO TRANSFER
$articles = $this->Article->query(
"SELECT articleID,subcategoryID,article_title,article_html,article_date,image_extension FROM tblnewsarticles LIMIT 1;"
);
foreach($articles as $article){
$data['Article']['id'] = $article['tblnewsarticles']['articleID'];
$data['Article']['published'] = 1;
$data['Article']['title'] = $article['tblnewsarticles']['article_title'];
$data['Article']['body'] = $article['tblnewsarticles']['article_html'];
$data['Article']['main_image'] = 'cat_'.$article['tblnewsarticles']['subcategoryID'].'/'.$article['tblnewsarticles']['articleID'].'.'.$article['tblnewsarticles']['image_extension'];
$data['Article']['category_id'] = $article['tblnewsarticles']['subcategoryID'];
$data['Article']['user_id'] = 2;
$data['Article']['created'] = $article['tblnewsarticles']['article_date'];
if (!empty($data)) {
$this->Article->create();
$this->Article->save($data);
$this->Session->setFlash('Migration processed successfully');
}else{
$this->Session->setFlash('Unsuccessful');
}
}
}
======== THIS IS WHAT I DID THAT WORKED FOR ME ================
function migrateArticle(){
$articles = $this->Article->query(
"SELECT articleID,subcategoryID,article_title,article_html,article_date,image_extension FROM tblnewscategories;"
);
foreach($articles as $article){
$categoryID = $article['tblnewsarticles']['subcategoryID'];
$articleID = $article['tblnewsarticles']['articleID'];
$articleTitle = $article['tblnewsarticles']['article_title'];
$articleSlug = str_replace(" ","-",strtolower($articleTitle));
$articleBody = $article['tblnewsarticles']['article_html'];
$articleImage = 'cat_'.$article['tblnewsarticles']['subcategoryID'].'/'.$article['tblnewsarticles']['articleID'].'.'.$article['tblnewsarticles']['image_extension'];
$articleDate = $article['tblnewsarticles']['article_date'];
$this->Article->query("INSERT INTO categories (id, published, title, slug, body, main_image, category_id, user_id, created) VALUES('$articleID', '1', '$articleTitle', '$articleSlug', '$articleBody', '$articleImage', '$categoryID', '2', '$articleDate');");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要尝试一次加载 4000 条记录,然后单独处理它们。使用复制所有内容的单个数据库查询可能会更容易。类似的东西
(当然未经测试)
Don't try to load 4000 records all at once and then process them individually. It's probably easier with a single database query that copies everything. Something like
(untested, of course)