数据库中的文件状态存在Laravel问题



这段代码的主要思想是循环文件夹(files/(,从文件夹中获取所有文件(*.txt(,存储文件名、vei_code、date到file数据库表。然后获取文件内容并将其存储到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');
});
}

这里有一个轻微的语法错误:

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

您希望引用字段名,而不是的特定实例。试试这个:

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

我希望这能有所帮助!

最新更新