首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CakePHP-3.0将数据从一个表插入到另外两个表中

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

Stack Overflow用户
提问于 2017-10-16 18:20:29
回答 1查看 275关注 0票数 0

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

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

以及我的CakePHP数据库中的两个表:

资产(id、name、serial_number) AssetsAssignations(id,asset_id,ip,mac,is_machine)

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

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

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

在AssetsController中,我说:

代码语言:javascript
复制
    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']);
}    

请问怎么做?

EN

回答 1

Stack Overflow用户

发布于 2018-09-04 18:53:46

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

代码语言:javascript
复制
      <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中:

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46776720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档