首頁  >  文章  >  後端開發  >  在yii框架中掃描目錄下文件入資料庫的方法

在yii框架中掃描目錄下文件入資料庫的方法

不言
不言原創
2018-07-10 15:20:341660瀏覽

這篇文章主要介紹了關於在yii框架中掃描目錄下文件入資料庫的方法,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

需求:

帶yii框架下寫一個定時任務,掃描某一目錄$target下的json文件,並導入指定的數據庫中

#實現:

#1.把需要入庫的json檔案放在指定目錄$target下
2.執行定時任務,導入到mongo庫Vote_teacher中

/opt/modules/php/bin/php /www/web/protected/yiic.php import VoteTeacher

3.定時任務代碼:
/web/protected/commandsImportCommand.php

 /tmp/abc1.txt
 */
class ImportCommand extends CConsoleCommand 
{

    public $target='/www/web/html/import/';     //扫描的目录

    public $model;    //入库的数据库对象

    public function run($args) {

        if (isset($args[0])) {
            $this->showMessage("params error", 1);
            exit;
        }
        $this->model = $args[0];
        $this->import();
    }

    /**
     * 分析用户回答的信息并按格式入库vote_answer
     * @author      lizhihui
      * @date        2018-4-9
     */
    private function import() 
    {
        $files = scandir($this->target);
        //读取目录下文件
        foreach ($files as $key => $val) {
            $val = pathinfo($val);
            $extension = $val['extension'];
            //过滤掉非json格式
            if($extension!='json'){
                continue;
            }

            $filePath=$this->target.$val['basename'];
            $fp=fopen($filePath,'r');
            $content='';
            while (!feof($fp)){
                $content.=fread($fp, filesize($filePath)/10);//每次读出文件10分之1
            //进行处理
            }
            $arr=json_decode($content);
            if(empty($arr)){
                $this->showMessage("no data to import");
                die();
            }

            //实例化不同数据库
            $tag=true;
            foreach ($arr as $key => $val) {                
                //存储
                $aVal = (array)$val;
                $model = new $this->model;
                if($model instanceof SMongodb){//动态字段存储
                    foreach ($aVal as $k => $v) {
                        $model->$k=$v;
                    }
                }else{//非动态字段存储
                    $model->attributes=$aVal;
                }

                if(!$model->save()){
                    $tag=false;
                }else{
                    unset($model);    //销毁一个对象,再次使用的时候会new一个新的
                }
            }

        }
        if(!$tag){
            $this->showMessage("some error in import");
        }else{
            $this->showMessage('import success!');
        }
    }    


    /**
     * 信息输出
     * @author      lizhihui
      * @date        2018-4-9
     */
    private function showMessage($str, $err = 0) {
        if (!$str) {
            return false;
        }
        if ($err) {
            echo "[ERROR]";
        } else {
            echo "[SUCCESS]";
        }

        echo date("Y-m-d H:i:s", time()) . " " . $str . "\n";

    }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!

相關推薦:

Yii無法捕捉到異常的解決方法

以上是在yii框架中掃描目錄下文件入資料庫的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn