我有一個應用程序使用另一個php cms系統,我現在將其轉移到CakePHP 1.3。我已經到了一個地步,我需要將我的舊文章tblnewsarticles
轉移到我的新CakePHP表articles
。這些表格有不同的字段,但我已經能夠將我需要的所有字段映射到新表格。我在我的ArticlesController class
中創建了以下臨時migrate
函數來爲我執行此操作,但不能將數據保存到我的表中。一切似乎都沒有問題,包括我已經完成了debug($data)
和debug($articles)
的測試,它顯示了數據,但我無法保存任何內容。我看不出什麼毛病此所以任何幫助表示讚賞...CakePHP 1.3:如何遷移到我的文章Tabel - 功能
//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');");
}
}
爲什麼不這樣做在數據庫級別?它可能會少得多的代碼。 – 2012-01-17 21:15:37
數據庫級別?對不起,但你是什麼意思? – 2012-01-17 22:11:15
明白了..謝謝 – 2012-01-18 01:34:44