【发布时间】:2016-01-20 04:10:06
【问题描述】:
我的目标是根据每 24 小时更改一次的 CSV 文件填充我的数据库。我的理解是 Seeders 非常适合这个,甚至可以根据需要以编程方式调用。我整理了基本代码并使用了他们网站上的文档,但是它失败了,即使启用了调试,错误消息也没有什么意义。
我已经测试并确认数据库连接正常工作,还注释掉了除“Truncate”行之外的所有 Seeder 代码,遗憾的是,它似乎甚至没有被处理。如果我从它工作的路线内截断,从播种机的run() 截断它不会。
我该如何解决或至少解决这个问题?
路线:
Route::get('seed', function()
{
Artisan::call('db:seed');
});
播种机:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
public function run()
{
// Empty the 'PLAYERS' Table
DB::table('players')->truncate();
// Fetch the data from our CSV File
$data = $this->seedFromCSV('export.csv', ',');
foreach ($data as $player)
{
DB::table('players')->insert(
[
'name' => $player['First Name'] . " " . $player['Last Name'],
'team' => '0',
'position' => '0',
'played' => $player['Played'],
'injury_status' => $player['Injury Indicator'],
'injury_type' => $player['Injury Details'],
'fd_salary' => $player['Salary'],
'fd_fppg' => $player['FPPG'],
'opponent' => '0'
]
);
}
}
private function seedFromCSV($filename, $deliminator = ",")
{
if(!file_exists($filename) || !is_readable($filename))
{
return FALSE;
}
$header = NULL;
$data = array();
if(($handle = fopen($filename, 'r')) !== FALSE)
{
while(($row = fgetcsv($handle, 1000, $deliminator)) !== FALSE)
{
if(!$header) {
$header = $row;
} else {
$data[] = array_combine($header, $row);
}
}
fclose($handle);
}
return $data;
}
}
错误:
FatalErrorException in compiled.php line 2057:
Maximum execution time of 30 seconds exceeded
【问题讨论】:
-
为什么要调用特殊路由来为数据库做种?为什么不用cli命令
php artisan db:seed? -
我在共享主机上,Laravel 是通过 Softaculous 安装的。我无法运行直接命令,因为我没有权限这样做。
标签: php database laravel model