Cakephp 1.3:如何迁移到我的文章标签 - 功能

Posted

技术标签:

【中文标题】Cakephp 1.3:如何迁移到我的文章标签 - 功能【英文标题】:Cakephp 1.3: How to migrate into my Article Tabel - Function 【发布时间】:2012-01-17 20:45:44 【问题描述】:

我有一个使用另一个 php cms 系统的应用程序,我现在将其转移到 CakePHP 1.3。我已经到了需要将tblnewsarticles 中的所有旧文章转移到我的新CakePHP 表articles 的地步。这些表有不同的字段,但我已经能够将我需要的所有字段映射到我的新表。我在ArticlesController class 中创建了以下临时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');");   
       

【问题讨论】:

为什么不在数据库级别这样做呢?它可能会少得多的代码。 数据库级别?抱歉,您是什么意思? 【参考方案1】:

不要尝试一次加载 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;

(当然,未经测试)

【讨论】:

感谢 Arjan,根据您的建议,我能够完成我需要的工作....非常感谢...在 EDIT 中查看我的问题

以上是关于Cakephp 1.3:如何迁移到我的文章标签 - 功能的主要内容,如果未能解决你的问题,请参考以下文章

带有“hasAndBelongsToMany”的控制器的 CakePHP 条件查询

CakePHP 1.3 - 保存不相关模型的事务

Cakephp:如何使用迁移插入记录

CakePHP 1.3 未保存到数据库但 sql 语句正确且 insertID 正确增加

复选框的 CakePHP 1.3 数据类型

CakePhp 1.3 如何处理作为 HTTP post 请求接收的数据?