跨数据库连接 – 永夜 https://www.shuijingwanwq.com 没有不值得去解决的问题,也没有不值得去学习的技术! Wed, 22 Mar 2023 01:24:26 +0000 zh-Hans hourly 1 https://wordpress.org/?v=7.0 在 Laravel 6 Eloquent 中跨数据库 join 查询的实现 https://www.shuijingwanwq.com/2023/03/22/7507/ https://www.shuijingwanwq.com/2023/03/22/7507/#respond Wed, 22 Mar 2023 01:24:26 +0000 https://www.shuijingwanwq.com/?p=7507 浏览量: 82 1、表 table_a 在 A 数据库,表 table_b 在 B 数据库。两张表通过列 table_a.id 、table_b.table_a_id 关联在一起。 2、参考 高级 Join 语句:https://learnku.com/docs/laravel/6.x/queries/5171#211e4f 。参考:https://stackoverflow.com/questions/41423603/join-two-mysql-tables-in-different-databases-on-the-same-server-with-laravel-elo 。如图1
参考 高级 Join 语句:https://learnku.com/docs/laravel/6.x/queries/5171#211e4f 。参考:https://stackoverflow.com/questions/41423603/join-two-mysql-tables-in-different-databases-on-the-same-server-with-laravel-elo

图1



$databaseName1 = (new Model1())->getConnection()->getDatabaseName();
$tableName1 = (new Model1())->getTable();
$tableName2 = (new Model2())->getTable();

$databaseName2 = (new Model2())->getConnection()->getDatabaseName();
DB::join($databaseName1 . '.' . $tableName1, function($join) use ($databaseName1, $tableName1, $databaseName2, $tableName2) {
    $join->on($databaseName1 . '.' . $tableName1 . '.id', $databaseName2 . '.' . $tableName2 . '.table_id');
});


3、最终实现的代码如下


$dba = (new A())->getConnection()->getDatabaseName();
$tablea = (new A())->getConnection()->getTablePrefix() . (new A())->getTable();
$dbb = (new B())->getConnection()->getDatabaseName();
$tableb = (new B())->getConnection()->getTablePrefix() . (new B())->getTable();
DB::table($dba . '.' . $tablea)
	->leftJoin($dbb . '.' . $tableb, function ($join) use ($dba, $tablea, $dbb, $tableb) {
		$join->on($dba . '.' . $tablea . '.id', $dbb . '.' . $tableb . '.table_a_id');
	})
	->whereIn($dbb . '.' . $tableb . '.role', ['unpublished', 'main'])
	->whereNull($dba . '.' . $tablea . '.deleted_at')
	->count();


4、最终生成的 SQL 如下,符合预期。如图2
最终生成的 SQL 如下,符合预期

图2



select
  count(*) as aggregate
from
  `dba`.`table_a`
  left join `dbb`.`table_b` on `dba`.`table_a`.`id` = `dbb`.`table_b`.`table_a_id`
where
  `dbb`.`table_b`.`role` in ('unpublished', 'main')
  and `dba`.`table_a`.`deleted_at` is null


]]>
https://www.shuijingwanwq.com/2023/03/22/7507/feed/ 0
在 Laravel 6 中的跨数据库连接的模型关联 https://www.shuijingwanwq.com/2022/06/16/6627/ https://www.shuijingwanwq.com/2022/06/16/6627/#respond Thu, 16 Jun 2022 01:10:57 +0000 https://www.shuijingwanwq.com/?p=6627 浏览量: 144 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
获取关联属性时报错:SQLSTATE[42S02]: Base table or view not found: 1146 Table 'object_wp.wp_theme_installation' doesn't exist

图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
获取关联属性成功。结果打印如下,如果不存在对应的记录,则为 NULL

图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


 ]]>
https://www.shuijingwanwq.com/2022/06/16/6627/feed/ 0