没有不值得去解决的问题,也没有不值得去学习的技术!

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

获取关联属性时报错:SQLSTATE[42S02]: Base table or view not found: 1146 Table 'object_wp.wp_theme_installation' doesn't exist
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


 

PHP / Laravel / Yii2 老项目维护与长期技术支持

如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。

适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护

可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查

如需咨询,请联系我,并注明:PHP 维护咨询

联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理