在 Laravel 6 中的跨数据库连接的模型关联

1、模型 Theme 的关联如下

    /**
     * 获取与主题关联的主题安装记录
     */    public function themeInstallation()
    {
        return $this->hasOne('Modules\ThemeStoreDB\Entities\ThemeInstallation', 'wp_theme_id');
    }

2、模型 ThemeInstallation 的关联如下

    /**
     * 获取此主题安装所属 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

{
    "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 所在的连接

    /**
     * 获取与主题关联的主题安装记录
     */    public function themeInstallation()
    {
        return $this->setConnection('mysql')->hasOne('Modules\ThemeStoreDB\Entities\ThemeInstallation', 'wp_theme_id');
    }

6、模型 ThemeInstallation 的关联中设置连接为 表 theme 所在的连接

    /**
     * 获取此主题安装所属 WordPress 的主题
     * @return BelongsTo
     */    public function theme()
    {
        return $this->setConnection('wordpress')->belongsTo('App\Models\Theme', 'wp_theme_id');
    }

7、获取关联属性成功。结果打印如下,如果不存在对应的记录,则为 NULL。如图2

图2

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 如下

select
  *
from
  `theme_installation`
where
  `theme_installation`.`wp_theme_id` = 104
  and `theme_installation`.`wp_theme_id` is not null
limit
  1

 

永夜