在 Laravel 6 中删除嵌套的关联模型,跨多个数据库连接的情况下
1、查询关联时嵌套预加载的实现
$wpThemes = Theme::with(['themeInstallation.themeInstallationVersionPreset.themeInstallationTasks' => function ($query) { $query->orderBy('id', 'desc'); }])->withCount(['logs'])->orderBy('updated_at_gmt', 'desc')->get()->makeHidden('content')->toArray();
2、打印执行的 SQL 语句
select `wp_themes`.*, ( select count(*) from `wp_theme_logs` where `wp_themes`.`id` = `wp_theme_logs`.`theme_id` ) as `logs_count` from `wp_themes` order by `updated_at_gmt` desc; select * from `theme_installation` where `theme_installation`.`wp_theme_id` in ( 1, 2, 3, 4, 5, 135, 136, 137, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158 ); select * from `theme_installation_version_preset` where `theme_installation_version_preset`.`theme_installation_id` in ( 27, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48 ); select * from `theme_installation_task` where `theme_installation_task`.`theme_installation_version_preset_id` in ( 27, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48 ) order by `id` desc;
3、现在计划删除 wp_themes 表中的一条记录时,删除嵌套的关联模型(总计涉及到 3 个关联模型)。为何不在表之间设置级联外键关系,以便 DBMS 自动删除相关行,原因在于 wp_themes 在 A 数据库中,而 3 个关联模型在 B 数据库中。
4、最终实现如下
$theme = findOrFail($id); $theme->themeInstallation->themeInstallationVersionPreset->themeInstallationTasks()->delete(); $theme->themeInstallation->themeInstallationVersionPreset()->delete(); $theme->themeInstallation()->delete(); $theme->delete();
5、执行时报错:SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘object_store.themes’ doesn’t exist (SQL: delete from `themes` where `id` = 156) 。提示:wp_themes 在 B 数据库中不存在。实则 wp_themes 应该在 A 数据库中的。报错所在行:$theme->delete(); 。虽然关联的 3 个模型已经被成功删除。如图1
{ "message": "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'object_store.themes' doesn't exist (SQL: delete from `themes` where `id` = 156)", "code": "42S02", "status_code": 500, "debug": { "line": 669, "file": "E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php", "class": "Illuminate\\Database\\QueryException", "trace": { "previous": [ "#0 E:\\wwwroot\\object\\vendor\\doctrine\\dbal\\lib\\Doctrine\\DBAL\\Driver\\PDOConnection.php(87): Doctrine\\DBAL\\Driver\\PDO\\Exception::new(Object(PDOException))", "#1 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php(484): Doctrine\\DBAL\\Driver\\PDOConnection->prepare('delete from `th...')", "#2 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php(662): Illuminate\\Database\\Connection->Illuminate\\Database\\{closure}('delete from `th...', Array)", "#3 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php(629): Illuminate\\Database\\Connection->runQueryCallback('delete from `th...', Array, Object(Closure))", "#4 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php(495): Illuminate\\Database\\Connection->run('delete from `th...', Array, Object(Closure))", "#5 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php(440): Illuminate\\Database\\Connection->affectingStatement('delete from `th...', Array)", "#6 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Query\\Builder.php(2823): Illuminate\\Database\\Connection->delete('delete from `th...', Array)", "#7 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Builder.php(874): Illuminate\\Database\\Query\\Builder->delete()", "#8 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php(936): Illuminate\\Database\\Eloquent\\Builder->delete()", "#9 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php(907): Illuminate\\Database\\Eloquent\\Model->performDeleteOnModel()", "#10 E:\\wwwroot\\object\\app\\Services\\ThemeService.php(198): Illuminate\\Database\\Eloquent\\Model->delete()", "#11 E:\\wwwroot\\object\\app\\Http\\Controllers\\Admin\\API\\ThemeController.php(165): App\\Services\\ThemeService->delete(156)", "#12 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Controller.php(54): App\\Http\\Controllers\\Admin\\API\\ThemeController->destroy('156', Object(App\\Services\\ThemeService))", "#13 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction('destroy', Array)", "#14 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(219): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\Admin\\API\\ThemeController), 'destroy')", "#15 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(176): Illuminate\\Routing\\Route->runController()", "#16 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(681): Illuminate\\Routing\\Route->run()", "#17 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(130): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#18 E:\\wwwroot\\object\\vendor\\spatie\\laravel-permission\\src\\Middlewares\\RoleOrPermissionMiddleware.php(25): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#19 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Spatie\\Permission\\Middlewares\\RoleOrPermissionMiddleware->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure), 'manage_themes|a...')", "#20 E:\\wwwroot\\object\\app\\Http\\Middleware\\CheckStoreAdminStatus.php(45): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#21 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): App\\Http\\Middleware\\CheckStoreAdminStatus->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#22 E:\\wwwroot\\object\\app\\Http\\Middleware\\SwitchLanguage.php(28): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#23 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): App\\Http\\Middleware\\SwitchLanguage->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#24 E:\\wwwroot\\object\\vendor\\dingo\\api\\src\\Http\\Middleware\\Auth.php(55): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#25 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Dingo\\Api\\Http\\Middleware\\Auth->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#26 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\Middleware\\Authenticate.php(43): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#27 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Illuminate\\Auth\\Middleware\\Authenticate->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure), 'api')", "#28 E:\\wwwroot\\object\\vendor\\dingo\\api\\src\\Http\\Middleware\\PrepareController.php(45): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#29 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Dingo\\Api\\Http\\Middleware\\PrepareController->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#30 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(105): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#31 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(683): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))", "#32 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(658): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Dingo\\Api\\Http\\Request))", "#33 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(624): Illuminate\\Routing\\Router->runRoute(Object(Dingo\\Api\\Http\\Request), Object(Illuminate\\Routing\\Route))", "#34 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(613): Illuminate\\Routing\\Router->dispatchToRoute(Object(Dingo\\Api\\Http\\Request))", "#35 E:\\wwwroot\\object\\vendor\\dingo\\api\\src\\Routing\\Adapter\\Laravel.php(88): Illuminate\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))", "#36 E:\\wwwroot\\object\\vendor\\dingo\\api\\src\\Routing\\Router.php(518): Dingo\\Api\\Routing\\Adapter\\Laravel->dispatch(Object(Dingo\\Api\\Http\\Request), 'v1')", "#37 E:\\wwwroot\\object\\vendor\\dingo\\api\\src\\Http\\Middleware\\Request.php(126): Dingo\\Api\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))", "#38 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(130): Dingo\\Api\\Http\\Middleware\\Request->Dingo\\Api\\Http\\Middleware\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#39 E:\\wwwroot\\object\\app\\Http\\Middleware\\ChangeAppUrlMiddleware.php(23): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#40 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): App\\Http\\Middleware\\ChangeAppUrlMiddleware->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#41 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#42 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#43 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#44 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#45 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#46 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#47 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode.php(63): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#48 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#49 E:\\wwwroot\\object\\vendor\\fideloper\\proxy\\src\\TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#50 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Fideloper\\Proxy\\TrustProxies->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#51 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(105): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#52 E:\\wwwroot\\object\\vendor\\dingo\\api\\src\\Http\\Middleware\\Request.php(127): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))", "#53 E:\\wwwroot\\object\\vendor\\dingo\\api\\src\\Http\\Middleware\\Request.php(103): Dingo\\Api\\Http\\Middleware\\Request->sendRequestThroughRouter(Object(Dingo\\Api\\Http\\Request))", "#54 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Dingo\\Api\\Http\\Middleware\\Request->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#55 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(105): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))", "#56 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(145): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))", "#57 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(110): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))", "#58 E:\\wwwroot\\object\\public\\index.php(57): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))", "#59 {main}" ], "current": [ "#0 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php(629): Illuminate\\Database\\Connection->runQueryCallback('delete from `th...', Array, Object(Closure))", "#1 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php(495): Illuminate\\Database\\Connection->run('delete from `th...', Array, Object(Closure))", "#2 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php(440): Illuminate\\Database\\Connection->affectingStatement('delete from `th...', Array)", "#3 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Query\\Builder.php(2823): Illuminate\\Database\\Connection->delete('delete from `th...', Array)", "#4 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Builder.php(874): Illuminate\\Database\\Query\\Builder->delete()", "#5 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php(936): Illuminate\\Database\\Eloquent\\Builder->delete()", "#6 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php(907): Illuminate\\Database\\Eloquent\\Model->performDeleteOnModel()", "#7 E:\\wwwroot\\object\\app\\Services\\ThemeService.php(198): Illuminate\\Database\\Eloquent\\Model->delete()", "#8 E:\\wwwroot\\object\\app\\Http\\Controllers\\Admin\\API\\ThemeController.php(165): App\\Services\\ThemeService->delete(156)", "#9 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Controller.php(54): App\\Http\\Controllers\\Admin\\API\\ThemeController->destroy('156', Object(App\\Services\\ThemeService))", "#10 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction('destroy', Array)", "#11 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(219): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\Admin\\API\\ThemeController), 'destroy')", "#12 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(176): Illuminate\\Routing\\Route->runController()", "#13 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(681): Illuminate\\Routing\\Route->run()", "#14 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(130): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#15 E:\\wwwroot\\object\\vendor\\spatie\\laravel-permission\\src\\Middlewares\\RoleOrPermissionMiddleware.php(25): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#16 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Spatie\\Permission\\Middlewares\\RoleOrPermissionMiddleware->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure), 'manage_themes|a...')", "#17 E:\\wwwroot\\object\\app\\Http\\Middleware\\CheckStoreAdminStatus.php(45): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#18 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): App\\Http\\Middleware\\CheckStoreAdminStatus->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#19 E:\\wwwroot\\object\\app\\Http\\Middleware\\SwitchLanguage.php(28): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#20 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): App\\Http\\Middleware\\SwitchLanguage->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#21 E:\\wwwroot\\object\\vendor\\dingo\\api\\src\\Http\\Middleware\\Auth.php(55): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#22 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Dingo\\Api\\Http\\Middleware\\Auth->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#23 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\Middleware\\Authenticate.php(43): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#24 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Illuminate\\Auth\\Middleware\\Authenticate->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure), 'api')", "#25 E:\\wwwroot\\object\\vendor\\dingo\\api\\src\\Http\\Middleware\\PrepareController.php(45): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#26 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Dingo\\Api\\Http\\Middleware\\PrepareController->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#27 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(105): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#28 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(683): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))", "#29 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(658): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Dingo\\Api\\Http\\Request))", "#30 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(624): Illuminate\\Routing\\Router->runRoute(Object(Dingo\\Api\\Http\\Request), Object(Illuminate\\Routing\\Route))", "#31 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(613): Illuminate\\Routing\\Router->dispatchToRoute(Object(Dingo\\Api\\Http\\Request))", "#32 E:\\wwwroot\\object\\vendor\\dingo\\api\\src\\Routing\\Adapter\\Laravel.php(88): Illuminate\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))", "#33 E:\\wwwroot\\object\\vendor\\dingo\\api\\src\\Routing\\Router.php(518): Dingo\\Api\\Routing\\Adapter\\Laravel->dispatch(Object(Dingo\\Api\\Http\\Request), 'v1')", "#34 E:\\wwwroot\\object\\vendor\\dingo\\api\\src\\Http\\Middleware\\Request.php(126): Dingo\\Api\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))", "#35 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(130): Dingo\\Api\\Http\\Middleware\\Request->Dingo\\Api\\Http\\Middleware\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#36 E:\\wwwroot\\object\\app\\Http\\Middleware\\ChangeAppUrlMiddleware.php(23): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#37 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): App\\Http\\Middleware\\ChangeAppUrlMiddleware->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#38 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#39 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#40 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#41 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#42 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#43 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#44 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode.php(63): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#45 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#46 E:\\wwwroot\\object\\vendor\\fideloper\\proxy\\src\\TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#47 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Fideloper\\Proxy\\TrustProxies->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#48 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(105): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))", "#49 E:\\wwwroot\\object\\vendor\\dingo\\api\\src\\Http\\Middleware\\Request.php(127): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))", "#50 E:\\wwwroot\\object\\vendor\\dingo\\api\\src\\Http\\Middleware\\Request.php(103): Dingo\\Api\\Http\\Middleware\\Request->sendRequestThroughRouter(Object(Dingo\\Api\\Http\\Request))", "#51 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Dingo\\Api\\Http\\Middleware\\Request->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))", "#52 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(105): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))", "#53 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(145): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))", "#54 E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(110): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))", "#55 E:\\wwwroot\\object\\public\\index.php(57): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))", "#56 {main}" ] } } }
6、虽然关联的 3 个模型已经被成功删除。如图2
7、打印 $theme 对象,发现其连接已经被修改为默认的 B 数据库所对应的连接。并非模型对象中默认的 A 数据库所对应的连接。如图3
8、决定在删除 $theme 对象前,重新设置其连接为 A 。最终实现如下
$theme->themeInstallation->themeInstallationVersionPreset->themeInstallationTasks()->delete(); $theme->themeInstallation->themeInstallationVersionPreset()->delete(); $theme->themeInstallation()->delete(); $theme->logs()->delete(); $theme->customPages()->delete(); $theme->setConnection('wordpress')->delete();
9、生成的删除 SQL 不再报错。分别查看 A、B 数据库中的数据,确认已被删除。如图4
select * from `wp_themes` where `wp_themes`.`id` = 157 limit 1; select * from `theme_installation` where `theme_installation`.`wp_theme_id` in (157); select * from `theme_installation_version_preset` where `theme_installation_version_preset`.`theme_installation_id` = 47 and `theme_installation_version_preset`.`theme_installation_id` is not null limit 1; delete from `theme_installation_task` where `theme_installation_task`.`theme_installation_version_preset_id` = 47 and `theme_installation_task`.`theme_installation_version_preset_id` is not null; delete from `theme_installation_version_preset` where `theme_installation_version_preset`.`theme_installation_id` = 47 and `theme_installation_version_preset`.`theme_installation_id` is not null; delete from `theme_installation` where `theme_installation`.`wp_theme_id` = 157 and `theme_installation`.`wp_theme_id` is not null; delete from `wp_themes` where `id` = 157;
10、当一些关联不存在时,会报错:Trying to get property ‘themeInstallationVersionPreset’ of non-object。原因在于 $theme->themeInstallation 为 null。所以需要判断关联对象是否存在。如图5
11、最终实现如下
if (isset($theme->themeInstallation)) { $theme->themeInstallation->themeInstallationVersionPreset->themeInstallationTasks()->delete(); $theme->themeInstallation->themeInstallationVersionPreset()->delete(); $theme->themeInstallation()->delete(); } $theme->setConnection('wordpress')->delete();
12、$theme->themeInstallation 为 null 时生成的删除 SQL 不再报错。
select * from `wp_themes` where `wp_themes`.`id` = 135 limit 1; select * from `theme_installation` where `theme_installation`.`wp_theme_id` in (135); delete from `wp_themes` where `id` = 135;
13、$theme->themeInstallation 不为 null 时生成的删除 SQL 也不再报错。如图6
select * from `wp_themes` where `wp_themes`.`id` = 143 limit 1; select * from `theme_installation` where `theme_installation`.`wp_theme_id` in (143); select * from `theme_installation_version_preset` where `theme_installation_version_preset`.`theme_installation_id` = 33 and `theme_installation_version_preset`.`theme_installation_id` is not null limit 1; delete from `theme_installation_task` where `theme_installation_task`.`theme_installation_version_preset_id` = 33 and `theme_installation_task`.`theme_installation_version_preset_id` is not null; delete from `theme_installation_version_preset` where `theme_installation_version_preset`.`theme_installation_id` = 33 and `theme_installation_version_preset`.`theme_installation_id` is not null; delete from `theme_installation` where `theme_installation`.`wp_theme_id` = 143 and `theme_installation`.`wp_theme_id` is not null; delete from `wp_themes` where `id` = 143;
近期评论