【问题标题】:cant import data csv to database in yii2无法将数据csv导入yii2中的数据库
【发布时间】:2015-10-22 03:48:46
【问题描述】:

我对 Web 开发非常陌生。

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

我对代码有什么错误感到困惑,代码会将数据数组 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 \yii\db\ActiveRecord
{
    public static function tableName()
    {
        return 'skt';
    }

    public $file;

    public function rules()
    {
        return [
            [['file'], 'required'],
            [['file'], 'file', 'extensions' => 'csv', 'maxSize' => 1024*1024*5],
            [['no_surat'], 'required'],
            [['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'
        ];
    }
}

感谢您的帮助..

【问题讨论】:

标签: csv import yii2


【解决方案1】:

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

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

getErrors() from Api

更好的方法是使用异常在导入时抛出和捕获错误。取决于您是否要跳过错误的 csv 行。

【讨论】:

    【解决方案2】:

    终于可以改变这个$hasil = explode(";", $data);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-17
      • 2011-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-26
      • 2013-11-04
      • 1970-01-01
      相关资源
      最近更新 更多