加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

无法在yii2中将数据csv导入数据库

发布时间:2020-12-13 22:24:21 所属栏目:PHP教程 来源:网络整理
导读:我对Web开发很新. 我在这里是新手,我在stackoverflow中的第一个问题.. 我很困惑代码上的错误,Code会将数据存储数据csv存储到数据库中, 抱歉,我的英语不好. 调节器 public function actionUpload(){ $model = new Skt(); //error_reporting(E_ALL); //ini_set
我对Web开发很新.

我在这里是新手,我在stackoverflow中的第一个问题..

我很困惑代码上的错误,Code会将数据存储数据csv存储到数据库中,
抱歉,我的英语不好.

调节器

public function actionUpload()
{
    $model = new Skt();
    //error_reporting(E_ALL);
    //ini_set('display_error',1);

    if ($model->load(Yii::$app->request->post())) {

        $file = UploadedFile::getInstance($model,'file');
        $filename = 'Data.' . $file->extension;
        $upload = $file->saveAs('uploads/' . $filename);

        if ($upload) {

            define('CSV_PATH','uploads/');
            $csv_file = CSV_PATH . $filename;
            $filecsv = file($csv_file);

            foreach ($filecsv as $data) {
                $modelnew = new Skt();
                $hasil = explode(",",$data);
                $no_surat= $hasil[0];
                $posisi= $hasil[1];
                $nama= $hasil[2];
                $tgl_permanen= $hasil[3];
                $grade= $hasil[4];
                $tgl_surat= $hasil[5];
                $from_date = $hasil[6];
                $to_date = $hasil[7];
                $modelnew->no_surat = $no_surat;
                $modelnew->posisi = $posisi;
                $modelnew->nama = $nama;
                $modelnew->tgl_permanen = $tgl_permanen;
                $modelnew->grade = $grade;
                $modelnew->tgl_surat = $tgl_surat;
                $modelnew->from_date = $from_date;
                $modelnew->to_date = $to_date;
                $modelnew->save();
                //print_r($modelnew->validate());exit;

            }
            unlink('uploads/'.$filename);
            return $this->redirect(['site/index']);
        }
    }else{
        return $this->render('upload',['model'=>$model]);
    }
    return $this->redirect(['upload']);
}

模型

class Skt extends yiidbActiveRecord
{
    public static function tableName()
    {
        return 'skt';
    }

    public $file;

    public function rules()
    {
        return [
            [['file'],'required'],[['file'],'file','extensions' => 'csv','maxSize' => 1024*1024*5],[['no_surat'],[['tgl_surat','from_date','to_date'],'string'],[['no_surat','posisi','nama','tgl_permanen','grade'],'string','max' => 255],];
    }
    public function attributeLabels()
    {
        return [
            'no_surat' => 'No Surat','posisi' => 'Posisi','nama' => 'Nama','tgl_permanen' => 'Tgl Permanen','grade' => 'Grade','tgl_surat' => 'Tgl Surat','from_date' => 'From Date','to_date' => 'To Date','file' => 'Select File'
        ];
    }
}

谢谢你帮忙..

解决方法

将代码更改为以下内容以输出尝试保存时可能发生的错误.根据您的模型规则,可能会发生错误.

if (!$modelnew->save()) {
   var_dump($modelnew->getErrors());
}

getErrors() from Api

更好的方法是使用异常来导入和捕获导入错误.取决于你是否想要跳过错误的csv行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读