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

无法将csv数据导入yii2中的数据库,csv,import,yii2,Csv,Import,Yii2,我对网络开发非常陌生 我是这里的新手,我在stackoverflow的第一个问题 我不知道代码上有什么错误,代码将存储数据数组csv到数据库, 对不起,我的英语不好 控制器 public function actionUpload() { $model = new Skt(); //error_reporting(E_ALL); //ini_set('display_error', 1); if ($model->load(Yii::$app->re

我对网络开发非常陌生

我是这里的新手,我在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'
        ];
    }
}

感谢您的帮助。

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

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


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

最后,它将使用更改此
$hasil=explode(“;”,$data)

您遇到了什么错误??发布错误或附加错误屏幕截图这可能有助于lotPHP使用CSV的本机功能。