如何为一个表单插入多个表添加在cakephp中

时间:2015-02-20 11:28:22

标签: cakephp

所以我想为3个表创建一个表单添加,但我不明白这个scipt,我正在谷歌搜索但是,我不知道,任何人都可以帮助我吗?

表pln_nontaglish_suspect

 | id | trx_id | no_regristrasi | tgl_regristrasi | nama | id_pel | rptag | switching_ref | admin_charge | iso | xml | inbox_id | telepon | info | jenis_transaksi | tagihan | partnet_cid | merchant | pln_ref |  bank_code |dt_trx | reprint | create_date|

表突变

| id | amount | note | jenis | inbox_id | balance | create_date | create_by | update_date | update_by | user_id | version |

表格交易

| id | user_id | product_id | destination | sender | sender_type | create_date | price_sell | price_buy | inbox_id | status | remark | saldo_awal | sn | receiver | resend

格式为add.ctp

<div id="page-container" class="row">

<div id="page-content" class="col-sm-9">

    <!--<h2><?php echo __('Add Pln Nontaglist Suspect'); ?></h2>-->

    <div class="plnNontaglistSuspects form">

        <?php echo $this->Form->create('PlnNontaglistSuspect', array('role' => 'form')); ?>

        <table>
            <div style="display:none">
            <?php

                $now = new DateTime();
                $today = $now->format("Y-m-d H:i:s");

                $user = $this->Session->read('Auth.User.username');

                $id = 'user_id';

                $user_id = $_POST['Transaction.user_id'];
                $inbox_id = $_POST['PlnNontaglistSuspect.inbox_id'];


                echo $this->Form->input('PlnNontaglistSuspect.trx_no');
                echo $this->Form->input('PlnNontaglistSuspect.idpel');
                echo $this->Form->input('PlnNontaglistSuspect.rptag');
                echo $this->Form->input('PlnNontaglistSuspect.admin_charge');
                echo $this->Form->input('PlnNontaglistSuspect.xml');
                echo $this->Form->input('PlnNontaglistSuspect.telpon');
                echo $this->Form->input('PlnNontaglistSuspect.info');
                echo $this->Form->input('PlnNontaglistSuspect.merchant');
                echo $this->Form->input('PlnNontaglistSuspect.pln_ref');
                echo $this->Form->input('PlnNontaglistSuspect.bank_code');
                echo $this->Form->input('PlnNontaglistSuspect.dt_trx');
                echo $this->Form->input('PlnNontaglistSuspect.reprint');
                echo $this->Form->input('PlnNontaglistSuspect.create_date', array('value' => $today));

                //input for table mutations
                echo $this->Form->input('Mutation.note');
                echo $this->Form->input('Mutation.jenis');
                echo $this->Form->input('Mutation.inbox_id', array('value' => $inbox_id));
                echo $this->Form->input('Mutation.balance');
                echo $this->Form->input('Mutation.create_date', array('value'=>$today));
                echo $this->Form->input('Mutation.create_by', array('value' => $user));
                echo $this->Form->input('Mutation.update_date');
                echo $this->Form->input('Mutation.update_by');
                echo $this->Form->input('Mutation.user_id', array('value' => $user_id));
                echo $this->Form->input('Mutation.version');

                //input for table transactions
                //echo $this->Form->input('Transaction.user_id', array('value' => $id));
                echo $this->Form->input('Transaction.destination');
                echo $this->Form->input('Transaction.sender');
                echo $this->Form->input('Transaction.sender_type');
                echo $this->Form->input('Transaction.create_date', array('value' => $today ));
                echo $this->Form->input('Transaction.price_buy');
                echo $this->Form->input('Transaction.status');
                echo $this->Form->input('Transaction.saldo_awal');
                echo $this->Form->input('Transaction.sn');
                echo $this->Form->input('Transaction.receiver');
                echo $this->Form->input('Transaction.resend');


            ?>

            </div>
            <legend>Tambah Nontaglish</legend>

            <tr>
                <td>No Regristrasi</td>
                <td>&nbsp;&nbsp; : &nbsp;&nbsp;</td>
                <td><?php echo $this->Form->input('PlnNontaglistSuspect.no_registrasi', array('label' => false,'class' => 'form-control')); ?></td>
            </tr>
            <tr>
                <td>Tgl Regristrasi</td>
                <td>&nbsp;&nbsp; : &nbsp;&nbsp;</td>
                <td><?php echo $this->Form->input('PlnNontaglistSuspect.tgl_registrasi', array('label' => false,  'class' => 'form-control')); ?></td>
            </tr>
            <tr>
                <td>Inbox Id</td>
                <td>&nbsp;&nbsp; : &nbsp;&nbsp;</td>
                <td><?php echo $this->Form->input('PlnNontaglistSuspect.inbox_id', array('label' => false, 'type' => 'text', 'class' => 'form-control')); ?></td>
            </tr>
            <tr>
                <td>Produk Id</td>
                <td>&nbsp;&nbsp; : &nbsp;&nbsp;</td>
                <td><?php echo $this->Form->input('Transaction.product_id', array('label' => false, 'empty' => '----Select Produk Id----', 'class' => 'form-control')); ?></td>
            </tr>
            <tr>
                <td>User Id</td>
                <td>&nbsp;&nbsp; : &nbsp;&nbsp;</td>
                <td><?php echo $this->Form->input('Transaction.user_id', array('label' => false, 'empty' => '----Select User Id----','class' => 'form-control'));?></td>
            </tr>
            <tr>
                <td>Nama Pelanggan</td>
                <td>&nbsp;&nbsp; : &nbsp;&nbsp;</td>
                <td><?php echo $this->Form->input('PlnNontaglistSuspect.nama', array('label' => false, 'class' => 'form-control')); ?></td>
            </tr>
            <tr>
                <td>Price Sell</td>
                <td>&nbsp;&nbsp; : &nbsp;&nbsp;</td>
                <td><?php echo $this->Form->Input('Transaction.price_sell', array('label' => false, 'class' => 'form-control'));?></td>
            </tr>
            <tr>
                <td>Iso</td>
                <td>&nbsp;&nbsp; : &nbsp;&nbsp;</td>
                <td><?php echo $this->Form->input('PlnNontaglistSuspect.iso', array('label' => false,'class' => 'form-control')); ?></td>
            </tr>
            <tr>
                <td>Switching Ref</td>
                <td>&nbsp;&nbsp; : &nbsp;&nbsp;</td>
                <td><?php echo $this->Form->input('PlnNontaglistSuspect.switching_ref', array('label' => false, 'class' => 'form-control')); ?></td>
            </tr>
            <tr>
                <td>Tagihan</td>
                <td>&nbsp;&nbsp; : &nbsp;&nbsp;</td>
                <td><?php echo $this->Form->input('PlnNontaglistSuspect.tagihan', array('label' => false,'class' => 'form-control')); ?></td>
            </tr>
            <tr>
                <td>Partner Cid</td>
                <td>&nbsp;&nbsp; : &nbsp;&nbsp;</td>
                <td><?php echo $this->Form->input('PlnNontaglistSuspect.partner_cid', array('label' => false, 'class' => 'form-control')); ?></td>
            </tr>
            <tr>
                <td>Amount</td>
                <td>&nbsp;&nbsp; : &nbsp;&nbsp;</td>
                <td><?php echo $this->Form->input('Mutation.amount', array('label' => false, 'class' => 'form-control'));?></td>
            </tr>
            <tr>
                <td>Remark</td>
                <td>&nbsp;&nbsp; : &nbsp;&nbsp;</td>
                <td><?php echo $this->Form->input('Transaction.remark', array('label' => false, 'class' => 'form-control'));?></td>
            </tr>
            <tr>
                <td>Jenis Transaksi</td>
                <td>&nbsp;&nbsp; : &nbsp;&nbsp;</td>
                <td><?php echo $this->Form->input('PlnNontaglistSuspect.jenis_transaksi', array('label' => false,'class' => 'form-control')); ?></td>
            </tr>
        </table>
        <?php echo $this->Form->submit('Submit', array('class' => 'btn btn-large btn-primary')); ?>


        <?php echo $this->Form->end(); ?>

    </div><!-- /.form -->

</div><!-- /#page-content .col-sm-9 -->

并在控制器中

public function add() {
    $this->loadModel('Product');
    $this->loadModel('User');
    if ($this->request->is('post')) {
        $this->PlnNontaglistSuspect->create();
        if ($this->PlnNontaglistSuspect->save($this->request->data)) {
            $this->Session->setFlash(__('The pln nontaglist suspect has been saved'), 'flash/success');
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The pln nontaglist suspect could not be saved. Please, try again.'), 'flash/error');
        }
    }

    //$inboxes = $this->PlnNontaglistSuspect->Inbox->find('list');
    $products = $this->Product->find('list');
    $users = $this->User->find('list',array(
            'fields'=>array('User.id','User.username'),
            'order'=>array('User.username')                
            )
    );
    $this->set(compact('products','users'));
}

1 个答案:

答案 0 :(得分:1)

仔细观察一下你的模型似乎没有关联,所以saveAll或saveAssociated在这种情况下不起作用。

如果您在PlnNontaglistSuspect模型中,则需要加载Mutation和Transaction Models并单独保存。

示例代码:

public function add() {
    //only try and save when the user has posted something
    if ($this->request->is('post')) {

        //in this example we are in the Mutation model
        // so load up Suspect and Transaction
        $this->loadModel('Suspect');
        $this->loadModel('Transaction');

        //create and save our Mutation record
        $this->Mutation->create();
        $this->Mutation->save($this->request->data);

        //create and save our Suspect record
        $this->Suspect->create();
        $this->Suspect->save($this->request->data);

        //create and save our Transaction record
        $this->Transaction->create();
        $this->Transaction->save($this->request->data);

        //set a Flash message and redirect the user
        $this->Session->setFlash(__('The mutation has been saved.'));
        return $this->redirect(array('action' => 'index'));

      }//end of if is post conditional

    }//end of add function

(请注意,这不是生产就绪 - 如果三种型号中的任何一种都没有保存,您需要添加错误处理。由于您在一次操作中保存三个单独的型号,您需要考虑一下如何处理这个问题。)

如果模型相关,您可以使用单个saveAssociated或saveAll方法,如下所述:http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array

最后一点 - 如果您坚持蛋糕惯例,您将使您的生活更轻松。例如,您的一个表是pln_nontaglish_suspect,考虑将其重命名为pln_nontaglish_suspects,因为Cake期望按惯例使用多个表名。此外,您在表中有一个create_date列,如果您将其更改为created(datetime),Cake将自动填充保存时的时间,同样您可以添加修改后的(datetime)列,Cake将记录任何更新的时间在这一栏中。

更多信息: http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html 和: http://book.cakephp.org/2.0/en/models/saving-your-data.html#using-created-and-modified

当你使用内置的约定和帮助时,Cake会变得更加强大。