在 Laravel 6 中的跨数据库连接的模型关联
1、模型 Theme 的关联如下
1 2 3 4 5 6 7 | /** * 获取与主题关联的主题安装记录 */ public function themeInstallation() { return $this ->hasOne( 'Modules\ThemeStoreDB\Entities\ThemeInstallation' , 'wp_theme_id' ); } |
2、模型 ThemeInstallation 的关联如下
1 2 3 4 5 6 7 8 | /** * 获取此主题安装所属 WordPress 的主题 * @return BelongsTo */ public function theme() { return $this ->belongsTo( 'App\Models\Theme' , 'wp_theme_id' ); } |
3、获取关联属性时报错:SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘object_wp.wp_theme_installation’ doesn’t exist 。如图1
1 2 3 4 5 6 7 8 9 10 11 | { "message": "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'object_wp.wp_theme_installation' doesn't exist (SQL: select * from `wp_theme_installation` where `wp_theme_installation`.`wp_theme_id` = 105 and `wp_theme_installation`.`wp_theme_id` is not null limit 1)", "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": ... } } |
4、表 theme_installation 不存在于 object_wp.wp_theme_installation,实际存在于 object_store.theme_installation
5、模型 Theme 的关联中设置连接为 表 theme_installation 所在的连接
1 2 3 4 5 6 7 | /** * 获取与主题关联的主题安装记录 */ public function themeInstallation() { return $this ->setConnection( 'mysql' )->hasOne( 'Modules\ThemeStoreDB\Entities\ThemeInstallation' , 'wp_theme_id' ); } |
6、模型 ThemeInstallation 的关联中设置连接为 表 theme 所在的连接
1 2 3 4 5 6 7 8 | /** * 获取此主题安装所属 WordPress 的主题 * @return BelongsTo */ public function theme() { return $this ->setConnection( 'wordpress' )->belongsTo( 'App\Models\Theme' , 'wp_theme_id' ); } |
7、获取关联属性成功。结果打印如下,如果不存在对应的记录,则为 NULL。如图2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | NULL object(Modules\ThemeStoreDB\Entities\ThemeInstallation)#3984 (26) { ["table":protected]=> string(18) "theme_installation" ["attributes":protected]=> array(11) { ["id"]=> int(2) ["theme_store_theme_id"]=> int(9) ["theme_id"]=> string(36) "96653e3a-9f95-44dd-ade9-d928a59d0332" ["wp_theme_id"]=> int(104) ["theme_name"]=> string(8) "theme" ["theme_custom_name"]=> string(8) "theme" ["role"]=> string(11) "unpublished" ["processing"]=> int(0) ["processing_failed"]=> int(1) ["created_at"]=> string(19) "2022-05-27 09:43:18" ["updated_at"]=> string(19) "2022-05-27 09:43:22" } ["fillable":protected]=> array(1) { [0]=> string(8) "theme_id" } ["connection":protected]=> string(5) "mysql" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["original":protected]=> array(11) { ["id"]=> int(2) ["theme_store_theme_id"]=> int(9) ["theme_id"]=> string(36) "96653e3a-9f95-44dd-ade9-d928a59d0332" ["wp_theme_id"]=> int(104) ["theme_name"]=> string(8) "theme" ["theme_custom_name"]=> string(8) "theme" ["role"]=> string(11) "unpublished" ["processing"]=> int(0) ["processing_failed"]=> int(1) ["created_at"]=> string(19) "2022-05-27 09:43:18" ["updated_at"]=> string(19) "2022-05-27 09:43:22" } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } } |
8、执行的 SQL 如下
1 2 3 4 5 6 7 8 9 | select * from `theme_installation` where `theme_installation`.`wp_theme_id` = 104 and `theme_installation`.`wp_theme_id` is not null limit 1 |
近期评论