anonymous
anonymous

Reputation: 1

How to insert multiple table for one form add in cakephp

so i want to create one form add for 3 tables, but i dont understand the scipt, i am googling but, i dont know, can anyone help me?

Table 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|

Table Mutations

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

Table Transactions

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

in the form 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 -->

And In the Controller

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'));
}

Upvotes: 0

Views: 1880

Answers (1)

Simon Mason
Simon Mason

Reputation: 561

Having had a closer look your Models do not appear to be related, so saveAll or saveAssociated will not work in this case.

If you are in the PlnNontaglistSuspect Model you will need to load up your Mutation and Transaction Models and save them separately.

Example code:

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

(Please note this is not production ready - you need to add error handling in case any one of the three Models doesn't save. As you are saving three separate Models in one operation you will need to give some thought to how to approach this).

If the Models were related you could use a single saveAssociated or saveAll method as described here: http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array

One final point - you will make your life a lot easier if you stick to Cake conventions. For example one of your tables is pln_nontaglish_suspect, consider renaming this to pln_nontaglish_suspects as Cake expects plural table names by convention. Also, you have a create_date column in your tables, if you change this to created (datetime) Cake will automagically populate this with the time at save, likewise you can add a modified (datetime) column and Cake will record the time of any updates in this column.

More here: http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html And: http://book.cakephp.org/2.0/en/models/saving-your-data.html#using-created-and-modified

Cake becomes far more powerful when you use the conventions and helpers built into it.

Upvotes: 1

Related Questions