Cakephp 1.3:如何迁移到我的文章表格 - 函数

发布于 2024-12-27 13:02:14 字数 3022 浏览 1 评论 0原文

我有一个使用另一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秉烛思 2025-01-03 13:02:14

不要尝试一次加载 4000 条记录,然后单独处理它们。使用复制所有内容的单个数据库查询可能会更容易。类似的东西

INSERT INTO articles
    (id, published, title, body,
     main_image,
     category_id, user_id, created)
    SELECT articleID, 1, article_title, article_html,
          CONCAT('cat_', subcategoryID, '/', articleID, '.', image_extension),
          subcategoryID, 2, article_date
    FROM tblnewsarticles;

(当然未经测试)

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

INSERT INTO articles
    (id, published, title, body,
     main_image,
     category_id, user_id, created)
    SELECT articleID, 1, article_title, article_html,
          CONCAT('cat_', subcategoryID, '/', articleID, '.', image_extension),
          subcategoryID, 2, article_date
    FROM tblnewsarticles;

(untested, of course)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文