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

时间:2017-10-11 17:43:14

标签: mysql cakephp cakephp-3.0

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

  

资产(id,serial_number,is_connected)

     

AssetsAssignations(id,name,asset_id,last_inventory_date)

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

  

计算机(id,serial_number,last_connected_date)

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

在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,我们会显示相同条目的ip_address和last_inventory_date。

所以,就像我试图将2个相似的表与基于同一列(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>

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

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

我的问题:这可能吗?以及如何做到这一点?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。在我的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 ?>
相关问题