【问题标题】:Laravel problem with file status from database来自数据库的文件状态的Laravel问题
【发布时间】:2020-06-19 08:06:11
【问题描述】:

这段代码的主要思想是循环文件夹(files/),从文件夹(*.txt)中取出所有文件,将文件名、vei_code、日期存储到文件数据库表中。然后获取该文件内容并将其存储到 Content 表等。 Content 和 File 表通过外键连接。如果对数据库的所有存储操作成功,则文件状态将更改为 1,否则将更改为 0,如果文件状态为 0,则将删除所有文件记录。但是,如果我想从数据库中查找所有文件状态为 0 我得到空数组。任何帮助都会非常有用。 代码:

  public function test(){
      try {
        // $url =" www.url.com/".;
        foreach (glob('files/*.txt') as $index => $path) {
            $filename = basename($path, '.txt');
            list($vei_id, $date, $type) = explode('_', $filename);
            $filename = $vei_id.'_'.$date;
            $content = array();

            $date = DateTime::createFromFormat('Ymd+', $date)->format('Y-m-d');

            // storing data to database
            $fileDB = new File();
            $fileDB->name = $filename;
            $fileDB->vei_id = $vei_id;
            $fileDB->file_date = $date;
            $fileDB->save();


            // files are csv files, read them line by line with fgetcsv
            if ( false !== $handle = fopen($path, 'rb') ) {
                while ( false !== $fields = fgetcsv($handle, 0, ';') ) {
                    array_pop($fields);
                    $content[] = $fields;
                }
                fclose($handle);
                try {
                  foreach ($content as $key => $data) {
                    $input = $fileDB->Contents()->create([
                      'file_id' => $fileDB->id,
                      // hard coded value, in the future I will create this record from other DB place
                      'vei_sn' => '23333',
                      'op_date' => $data[0],
                      'con_type' => $data[1],
                      'op_name' => $data[2],
                      'ecu_name' => $data[3],
                      'ecu_name2' => $data[4],
                    ]);
                  }

                  // THERE THE PROBLEM STARTS

                  //check if input was successful, change file status from 0 to 1
                  if ($input) {
                    $fileDB->status = '1';
                    $fileDB->save();
                  }

                  // find file status where status = 0
                  $fileFails = File::where($fileDB->status, '=', '0')->get();
                  print_r($fileFails);
                } catch (Exception $e) {

                }
                echo '<pre>', print_r($content, true), '</pre>';
            }
          }
        } catch (Exception $e) {
          echo 'Caught exception: ',  $e->getMessage(), "\n";
        }
      }

这是我的文件结构:

09/02/2020 11:03:58;OBD;FLASHRead;MCM;2;;
09/02/2020 11:05:09;OBD;EEPROMRead;MCM;2;;
09/02/2020 11:10:06;OBD;FLASHRead;ACM;2;;
09/02/2020 11:11:16;OBD;EEPROMRead;ACM;2;;
11/02/2020 08:31:36;OBD;EEPROMWrite;ACM;2.1;;
11/02/2020 08:36:07;OBD;EEPROMWrite;ACM;2.1;;
11/02/2020 08:42:55;OBD;FLASHWrite;ACM;2.1;;
12/02/2020 05:57:48;OBD;EEPROMRead;ACM;2;;
12/02/2020 06:05:00;OBD;FLASHWrite;ACM;2;;
12/02/2020 06:06:08;OBD;EEPROMRead;MCM;2;;

这是我的文件数据库表:

public function up()
    {
        Schema::create('files', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('file_date');
            $table->integer('status')->default('0');
            $table->string('vei_id', 8);
            $table->timestamps();
        });
    }

这是我的内容数据库表:

    public function up()
    {
        Schema::create('contents', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('file_id');
            $table->integer('vei_sn');
            $table->dateTime('op_date');
            $table->string('con_type');
            $table->string('op_name');
            $table->string('ecu_name');
            $table->string('ecu_name2');
            $table->integer('tokens')->nullable();
            $table->timestamps();

            $table->foreign('file_id')
                  ->references('id')->on('files')
                  ->onDelete('cascade');
        });
    }

【问题讨论】:

    标签: php database laravel file


    【解决方案1】:

    这里有一点语法错误:

    $fileFails = File::where($fileDB->status, '=', '0')->get();
    

    您要引用的字段名称而不是特定的实例。试试这个:

      $fileFails = File::where('status', '=', '0')->get();
    

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2021-02-08
      • 2019-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 2021-12-13
      • 1970-01-01
      相关资源
      最近更新 更多