CakePHP-3.0将数据从一个表插入到另外两个表中

时间:2017-10-16 18:20:30

标签: mysql cakephp-3.0

在我的应用程序中,我有一个表(存储在只读数据库中):

  

机器(ID,NAME,SERIAL_NUMBER,IP,MAC),大写。

和CakePHP数据库中的2个表:

  

资产(id,name,serial_number)

     

AssetsAssignations(id,asset_id,ip,mac,is_machine)

我创建了一个名为“retrieve_machines.ctp”的页面来读取Machines表:

1- foreach SERIAL_NUMBER,如果资产中不存在,则将其添加到资产。

2- foreach SERIAL_NUMBER,当SERIAL_NUMBER = AssetsAssignation-> Asset-> serial_number时,将(ip,mac)的新值复制到AssetsAssignations中(对于imporper算法,抱歉)。

在AssetsController中,我把它放在:

    public function retrieveMachines()
{
    $query = $this->Assets->find()
        ->contain([]);
    $filter = $this->Filter->prg($query);
    $assets = $this->paginate($filter, ['limit' => 50]);

    $connection = ConnectionManager::get('db2'); // 'db2' 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('assets', 'machines'));
    $this->set('_serialize', ['assets']);
}    

请问怎么做?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。在我的retrieve_machines.ctp中:

      <table class="hoverTable dataTable">
        <thead>
            <tr>
                <th>Model Number</th><th>Serial Number<th>MAC</th><th>IP</th><th scope="col" class="actions"><?= __('Actions') ?></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($machines as $machine):                    
                $model_number = $machine['MODEL'];
                $serial_number = $machine['SERIAL'];
                $mac = $machine['MAC'];
                $ip = $machine['IP'];
                $is_found = false;
                foreach ($assets as $asset):
                    if (strcasecmp($asset->serial_number, $serial_number) == 0) :
                        $is_found = true;
                    endif;
                endforeach;
                if(!$is_found):
            ?>                        
            <tr>
                <td><?= h($model_number) ?></td>
                <td><?= h($serial_number) ?></td>
                <td><?= h($mac) ?></td>
                <td><?= h($ip) ?></td>
                <td class="actions">
                            <?= $this->Html->iconButton('glyphicon-plus', __('Add to Assets'), [
                                    'controller' => 'Assets',
                                    'action' => 'add_from_db2', '?' => ['mnum' =>$model_number, 'snum' =>$serial_number]]);
                            ?>
                </td>                               
            </tr>
            <?php
                    endif;
                 endforeach; ?>
        </tbody>
    </table>

然后,在add_from_db2.ctp中:

<div>
    <?= $this->Form->create($asset) ?>
    <fieldset>
        <legend><?= __('Add Asset From DB2') ?></legend>
        <div class="row">
            <div class="col-xs-3"><?= $this->Form->input('model_number', array('default'=>$this->request->query['mnum'], 'autofocus'=>'autofocus')); ?></div>
            <div class="col-xs-3"><?= $this->Form->input('serial_number', array('default'=>$this->request->query['snum'])); ?></div>
        </div>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
</div>