Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP1.3:如何迁移到我的文章Tabel-函数_Php_Cakephp_View_Controller_Migration - Fatal编程技术网

CakePHP1.3:如何迁移到我的文章Tabel-函数

CakePHP1.3:如何迁移到我的文章Tabel-函数,php,cakephp,view,controller,migration,Php,Cakephp,View,Controller,Migration,我有一个使用另一个PHPCMS系统的应用程序,我现在将其转移到CakePHP1.3。我现在需要将tblnewsactures中的所有旧文章转移到新的CakePHP表articles。这些表有不同的字段,但我已经能够将所有需要的字段映射到我的新表。我在我的ArticlesController类中创建了以下临时migrate函数来为我执行此操作,但无法将数据保存到我的表中。一切似乎都正常,包括我已经用debug($data)和debug($articles)进行了测试,它显示了数据,但我无法保存任何

我有一个使用另一个PHPCMS系统的应用程序,我现在将其转移到CakePHP1.3。我现在需要将
tblnewsactures
中的所有旧文章转移到新的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');");   
    }   
}

不要试图一次加载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,根据你的建议,我能够完成我需要的。。。。谢谢。。。通过编辑查看我的问题