在我的应用程序中引用第二个数据库/数据源的问题

问题描述 投票:0回答:1

我在将第二个数据库合并到我的应用程序中时遇到问题。

  1. 我已在
    app.php
    app_local.php
  2. 中创建了两个连接
  3. 引用第二个数据库的模型(即
    BusinessRequests
    )实现了
    defaultConnectionName()

错误:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'app_a.business_requests' doesn't exist

生成的 SQL - 请注意在 LEFT JOIN 中引用

BusinessRequests
时缺少数据库/连接前缀

SELECT
  Requests.id AS Requests__id,
  Requests.business_request_id AS Requests__business_request_id,
  Requests.notes AS Requests__notes,
  Requests.created AS Requests__created,
  Requests.modified AS Requests__modified,
  BusinessRequests.id AS BusinessRequests__id,
  BusinessRequests.title AS BusinessRequests__title,
  BusinessRequests.status AS BusinessRequests__status,
  BusinessRequests.created AS BusinessRequests__created,
  BusinessRequests.modified AS BusinessRequests__modified
FROM
  requests Requests
  LEFT JOIN business_requests BusinessRequests ON BusinessRequests.id = Requests.business_request_id
WHERE
  Requests.id = 1
LIMIT
  1

数据库

app_a
(注意外键设置)

CREATE TABLE `requests` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `business_request_id` int(11) unsigned DEFAULT NULL,
  `notes` varchar(255) NOT NULL,
  `created` datetime NOT NULL DEFAULT current_timestamp(),
  `modified` datetime NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `FK_requests_app_common.business_requests` (`business_request_id`),
  CONSTRAINT `FK_requests_app_common.business_requests` FOREIGN KEY (`business_request_id`) REFERENCES `app_common`.`business_requests` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci

数据库

app_common

CREATE TABLE `business_requests` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `status` varchar(255) NOT NULL,
  `created` datetime NOT NULL DEFAULT current_timestamp(),
  `modified` datetime NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=70270 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci

app.php

的内容
  'Datasources' => [
    'default' => [
        'className' => Connection::class,
        'driver' => Mysql::class,
        'persistent' => false,
        'timezone' => 'UTC',
        'flags' => [],
        'cacheMetadata' => true,
        'log' => false,
        'quoteIdentifiers' => false,
    ],

    'common' => [
        'className' => Connection::class,
        'driver' => Mysql::class,
        'persistent' => false,
        'timezone' => 'UTC',
        'flags' => [],
        'cacheMetadata' => true,
        'log' => false,
        'quoteIdentifiers' => false,
    ],
],

app_local.php
的内容(mariadb是一个Docker容器)

'Datasources' => [
        'default' => [
            'host' => 'mariadb',
            'username' => 'root',
            'password' => 'root',
            'database' => 'app_a',
            'url' => env('DATABASE_URL', null),
        ],

        'common' => [
            'host' => 'mariadb',
            'username' => 'root',
            'password' => 'root',
            'database' => 'app_common',
            'url' => env('DATABASE_URL', null),
        ],
    ],

BusinessRequestsTable.php

的内容

这里我指定了模型使用的连接。

public static function defaultConnectionName(): string
{
    return 'common';
}

RequestsController.php

的内容
public function view($id = null)
{
    $request = $this->Requests->get($id, [
           'contain' => ['BusinessRequests'],
    ]);

    $this->set(compact('request'));
}
php cakephp cakephp-4.x
1个回答
0
投票

请在查询中指定哪个数据库属于哪个表。尝试这样的事情:

SELECT
    Requests.id AS Requests__id,
    Requests.business_request_id AS Requests__business_request_id,
    Requests.notes AS Requests__notes,
    Requests.created AS Requests__created,
    Requests.modified AS Requests__modified,
    BusinessRequests.id AS BusinessRequests__id,
    BusinessRequests.title AS BusinessRequests__title,
    BusinessRequests.status AS BusinessRequests__status,
    BusinessRequests.created AS BusinessRequests__created,
    BusinessRequests.modified AS BusinessRequests__modified
FROM
    app_a.requests Requests
    LEFT JOIN app_common.business_requests BusinessRequests ON BusinessRequests.id = Requests.business_request_id
WHERE
    Requests.id = 1
LIMIT
    1
© www.soinside.com 2019 - 2024. All rights reserved.