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

Cross-Database Model Relationships in Laravel 6

Error when retrieving the relationship property: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'object_wp.wp_theme_installation' doesn't exist

1. The relationship in the Theme model is as follows

PHP

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

2. The relationship in the ThemeInstallation model is as follows

PHP

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

3. An error occurs when retrieving the relationship property: SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘object_wp.wp_theme_installation’ doesn’t exist. See Figure 1

Error when retrieving the relationship property: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'object_wp.wp_theme_installation' doesn't exist
Figure 1
JSON

{
    "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. The table theme_installation does not exist in object_wp.wp_theme_installation; it actually exists in object_store.theme_installation

5. In the Theme model’s relationship, set the connection to the connection where the theme_installation table resides

PHP

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

6. In the ThemeInstallation model’s relationship, set the connection to the connection where the theme table resides

PHP

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

7. Retrieving the relationship property succeeds. The printed result is as follows; if no corresponding record exists, it returns NULL. See Figure 2

Retrieving the relationship property succeeds. The printed result is as follows; if no corresponding record exists, it returns NULL
Figure 2
Plaintext

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. The executed SQL is as follows

SQL

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

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系