Php Laravel队列未将实体保存到数据库
请帮帮我,我可能在做什么或理解什么不对。 我正在创建一个到数据库的大规模上传,这需要一点时间,所以我决定使用async all并在后台使用Laravel队列处理它 在控制器中,我创建了一个关联列表,并将其传递给Laravel作业Php Laravel队列未将实体保存到数据库,php,asynchronous,laravel-5,eloquent,Php,Asynchronous,Laravel 5,Eloquent,请帮帮我,我可能在做什么或理解什么不对。 我正在创建一个到数据库的大规模上传,这需要一点时间,所以我决定使用async all并在后台使用Laravel队列处理它 在控制器中,我创建了一个关联列表,并将其传递给Laravel作业 foreach($data as $row) { $instantArray = array(); $orderDate = \PHPExcel_Shared_Date::ExcelToPHPObject($row[3]);
foreach($data as $row)
{
$instantArray = array();
$orderDate = \PHPExcel_Shared_Date::ExcelToPHPObject($row[3]);
$issueDate = \PHPExcel_Shared_Date::ExcelToPHPObject($row[4]);
$orderRow = array(
'invoice_no' => trim($row[0]),
'agent_id' => $row[1],
'issued_at' => $orderDate->format('Y-m-d'),
'received_at' => $issueDate->format('Y-m-d'),
);
$gameArray = array();
$invoiceOrdersArray[] = $orderRow;
}
$job = (new Import($invoiceOrdersArray));
dispatch($job);
在Laravel的工作中,我试图创建一个Enty并将其上传到数据库
class Import implements ShouldQueue
{
use InteractsWithQueue, SerializesModels, Queueable, Dispatchable;
protected $importData;
public function __construct($importData)
{
$this->importData = $importData;
}
public function handle()
{
foreach($this->importData as $insert)
{
InvoicesOrder::create($insert);
}
}
但是,每次尝试时,我都可以在日志中看到:
#0 app\Utilities\Updater.php(18): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Trying to get p...', '...', 18, Array)
#1 vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php(348): Modules\Accounting\Entities\InvoicesOrder::App\Utilities\{closure}(Object(Modules\Accounting\Entities\InvoicesOrder))
#2 vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php(199): Illuminate\Events\Dispatcher->Illuminate\Events\{closure}('eloquent.creati...', Array)
#3 vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php(159): Illuminate\Events\Dispatcher->dispatch('eloquent.creati...', Array, true)
#4 vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasEvents.php(148): Illuminate\Events\Dispatcher->until('eloquent.creati...', Object(Modules\Accounting\Entities\InvoicesOrder))
#5 vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(636): Illuminate\Database\Eloquent\Model->fireModelEvent('creating')
#6 vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(522): Illuminate\Database\Eloquent\Model->performInsert(Object(Illuminate\Database\Eloquent\Builder))
#7 Modules\Accounting\Jobs\ImportFiles.php(39): Illuminate\Database\Eloquent\Model->save()
#8 [internal function]: Modules\Accounting\Jobs\ImportFiles->handle()
#9 vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(29): call_user_func_array(Array, Array)
#10 vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#11 vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#12 vendor\laravel\framework\src\Illuminate\Container\Container.php(539): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#13 vendor\laravel\framework\src\Illuminate\Bus\Dispatcher.php(94): Illuminate\Container\Container->call(Array)
#14 vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(114): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}(Object(Modules\Accounting\Jobs\ImportFiles))
#15 vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Modules\Accounting\Jobs\ImportFiles))
#16 vendor\laravel\framework\src\Illuminate\Bus\Dispatcher.php(98): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#17 vendor\laravel\framework\src\Illuminate\Queue\CallQueuedHandler.php(42): Illuminate\Bus\Dispatcher->dispatchNow(Object(Modules\Accounting\Jobs\ImportFiles), false)
#18 vendor\laravel\framework\src\Illuminate\Queue\Jobs\Job.php(69): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\RedisJob), Array)
#19 vendor\laravel\framework\src\Illuminate\Queue\Worker.php(317): Illuminate\Queue\Jobs\Job->fire()
每当我在控制器中执行时,它工作正常,只是加载时间很长。
可能无法使用队列在作业中保存实体
谢谢。在您的工作中,您可以在
$this->importData
属性中插入数据
但是在handle方法中,您正在循环使用$invoiceOrdersArray
,这是从哪里来的
你的环不是这样吗
foreach($this->importData as $insert)
{
InvoicesOrder::create($insert);
}
在handle方法中甚至不需要未使用的$order
变量
另外,我认为您需要在作业中使用InvoicesOrder
类。所以整个事情可能是这样的
use namespace/to/InvoicesOrder;
class Import implements ShouldQueue
{
use InteractsWithQueue, SerializesModels, Queueable, Dispatchable;
protected $importData;
public function __construct($importData)
{
$this->importData = $importData;
}
public function handle()
{
foreach($$this->importData as $insert)
{
InvoicesOrder::create($insert);
}
}
}
如果有人遇到类似的问题,请检查您的实体是否使用了任何类型的演示者(我使用的是presentable trait),它会添加诸如created/Update by之类的字段。我们从Auth::user()->id得到了什么。。。正在尝试获取非对象的属性,因为队列不是用户。由于队列工作进程是长期存在的进程,它们不会在不重新启动的情况下获取对代码的更改。因此,使用队列工作者部署应用程序的最简单方法是在部署过程中重新启动工作者。您可以通过发出queue:restart命令优雅地重新启动所有工作进程:
php artisan queue:restart
是的,更正了我的问题,作业中还有一些内容,但我只是删除了问题不需要的所有内容。堆栈跟踪显示它正在尝试在
Modules\Accounting\Entities\InvoicesOrder
命名空间中定位您的InvoiceOrder
,该命名空间与作业相同。这绝对正确吗?