将来自不同数据库的 2 个相似模型关联到同一模型

Associating 2 similar models from different databases to a same model

我正在使用 CakePHP 3.3.16 开发一个库存系统,使用 2 个数据库。我能够连接两个数据库并显示两者的内容。首先,我有 2 tables:

Assets (id, serial_number, is_connected)

AssetsAssignations (id, name, asset_id, last_inventory_date)

在第二个数据库中,我有一个table

Machines (id, serial_number, last_connected_date)

表示连接到 ip 地址的资产子集。所以一些序列号可以同时在资产和机器中。

在 MachinesController 索引中,我把这个:

    $connection = ConnectionManager::get('db2'); // where my second database is configured 
    $machines = $connection->execute('SELECT * FROM MACHINE');

    $this->set(compact('machines'));
    $this->set('_serialize', ['machines']);

在 AssetController 中,我把这个:

    $assets = $this->Assets->find();
    $this->set(compact('assets'));
    $this->set('_serialize', ['assets']);

在 AssetsAssignationsController 中,我暂时有这个:

    $query = $this->AssetsAssignations->find()
        ->contain(['Assets']);
    $filter = $this->Filter->prg($query);
    $assetsAssignations = $this->paginate($filter, ['limit' => 50]);
    
    $connection = ConnectionManager::get('db2'); // 'kace_db' where my second database is configured 
    $machines = $connection->execute('SELECT * FROM MACHINE ORDER BY NAME ASC');

    $this->set(compact('machines'));
    $this->set('_serialize', ['machines']);

    $this->set(compact('assetsAssignations', 'machines'));
    $this->set('_serialize', ['assetsAssignations']);

我需要在 AssetsAssignations index.ctp 中显示资产中的 asset_id、serial_number。如果我们在另一个数据库中有相同的 serial_number(在 table 机器中),我们将显示相同条目的 ip_address 和 last_inventory_date。

所以,就像我试图将 2 个相似的 table 与基于同一列 (serial_number) 的第 3 个相关联。

这是 AssetsAssignations index.ctp:

        <?php foreach ($assetsAssignations as $assetsAssignation): ?>
            <tr>
                <td><?= h($assetsAssignation->asset->serial_number) ?></td>
                <td><?= h($assetsAssignation->asset->is_connected) ?></td>

然后如果 Machines 中存在相同的 serial_number,我想在同一视图中显示它:

<td><?= h($assetsAssignation->something->last_conneced_date) ?></td>

我的问题:这可能吗?以及如何去做?

感谢您的帮助。

我找到了解决办法。在我的 index.ctp:

<?php 
                        $ip_address = $assetsAssignation->ip_address;
                        $mac_address = $assetsAssignation->mac_address;

                        $is_found = False;
                        foreach ($machines as $row) : 
                            if ($row['SERIAL_NUMBER'] == $assetsAssignation->asset->serial_number):
                                $ip_address = $row['IP'];                                
                                $mac_address = $row['MAC'];
                                $is_found = True;
                            endif;                                
                        endforeach; ?>
                <?php if($is_found): ?>
                    <td><?= h($ip_address) ?></td>
                    <td><?= h($mac_address) ?></td>
                <?php else: ?>
                    <td><span style="color:#fc352d;text-align:center;"><?= h($ip_address) ?></span></td>                
                    <td><span style="color:#fc352d;text-align:center;"><?= h($mac_address) ?></span></td>
                <?php endif ?>