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');");
}
}
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)