如何使用 Codeigniter 4 连接具有不同数据库的两个表

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

我正在尝试使用 codeigniter 4 连接两个具有不同数据库的表。我已经使用 $db1 和 $db2 声明了不同的数据库。我的问题是当我尝试加入时,它显示错误“表'db2.student'不存在”。但我尝试通过在没有连接的情况下在“db2”上运行一个简单的查询来测试这一点,结果是好的。

我的控制器(JoinController.php)

<?php
namespace App\Controllers;
use CodeIgniter\Controller;

class JoinController extends Controller
{
    public function index()
{
    // Load the databases
    $db1 = \Config\Database::connect('db1');
    $db2 = \Config\Database::connect('db2');

    // Load the model
    $model = new \App\Models\JoinModel($db1, $db2);

    // Get the joined result from the model
    $data['result'] = $model->getJoinedData();

    // Load the view with the data
    return view('join_view', $data);
    }
 }

我的模型(JoinModel.php)

<?php
namespace App\Models;

use CodeIgniter\Model;

class JoinModel extends Model
{
    protected $db1;
    protected $db2;

    public function __construct($db1, $db2)
    {
        // Set the databases
        $this->db1 = $db1;
        $this->db2 = $db2;
    }

    public function getJoinedData()
    {
        // Perform the join
        $query = $this->db1->table('attendance as k')
                ->select('k.id, k.time_in, k.time_out, s.student_name, s.programme')
                ->join('db2.student as s', 'k.id= s.id')
                ->get();

        // Get the result
        return $query->getResult();

        // Example query to test the connection to db2
        // $query = $this->db2->table('student')->get();
        // return $query->getResult();

        // var_dump($query);
    }
}

我的视图(join_view.php)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Joined Data</title>
</head>
<body>
    <h1>Joined Data</h1>

    <table border="1">
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
            <th>Column 3</th>
            <!-- Add more table headers as needed -->
        </tr>
        <?php foreach ($result as $row): ?>
            <tr>
                <td><?= $row->id ?></td>
                <td><?= $row->student_name ?></td>
                <td><?= $row->programme ?></td>
                <!-- Add more table cells as needed -->
            </tr>
        <?php endforeach; ?>
    </table>
</body>
</html>

我很感激您的帮助。谢谢你

codeigniter codeigniter-4
1个回答
0
投票

如果 db1db2 数据库 schemas 存在,并且 db1.attendancedb2.student 也存在,则您提供的查询应该有效。如果数据库模式位于不同的服务器上,查询将不起作用。

db1db2是数据库连接,而不是JoinController.php控制器中的数据库模式名称(当然模式名称可以与连接名称相同)!

确保 db1db2 连接到同一服务器,或者您可以使用如下替代解决方案。

一个连接可以连接到多个数据库模式,因此您可以连接表。为此,您不必有两个不同的连接。以下应该有效。

控制器

<?php

namespace App\Controllers;

use App\Models\JoinModel;
use CodeIgniter\Controller;

class Test extends Controller
{
    public function index()
    {
        // Load the model
        $model = model(JoinModel::class);

        // Get the joined result from the model
        $data['result'] = $model->getJoinedData();

        // Load the view with the data
        return view('join_view', $data);
    }
 }

型号

<?php

namespace App\Models;

use CodeIgniter\Model;

class JoinModel extends Model
{
    protected $table = 'attendance'; // use table name here

    public function getJoinedData()
    {
        // Perform the join, now you can omit the table name
        $query = $this->select('k.id, k.time_in, k.time_out, s.student_name, s.programme')
            ->join('db2.student as s', 'k.id= s.id')
            ->get();

        // Get the result
        return $query->getResult();
    }
}

注意:现在,您不必拥有构造函数。 但是,如果您的模型中确实需要 __constructor,请确保调用父级的 __constructor,否则您将失去模型的所有功能。或者,如果不需要,只需不扩展基本模型即可。

注意:如果您使用CodeIgniter内置工厂model方法,这是一个很好的做法。

如果数据库模式位于不同的服务器上,您可以分别查询数据,然后在 PHP 中将它们连接起来。根据 SQL 类型,有替代解决方案。例如,对于 MySQL,有 MySQL 联合存储引擎或 Oracle 的数据库链接。或者您可以简单地从一台服务器迁移到另一台服务器。

© www.soinside.com 2019 - 2024. All rights reserved.