0

I have 2 database let say DB A and DB B, here is the recap: DB A has:

 - tbl_a
 - tbl_b
 - tbl_c
 - tbl_d 

DB B has:

 - tbl_x
 - tbl_y

tbl_d has:

tbl_a.id, tbl_b.id, tbl_c.id, tbl_x.id, tbl_y.name

I want add data into tbl_d using cascade dropdown list I already success in making dropdown list to get list of tbl_a.id, tbl_b.id, tbl_c.id with tbl_a.id as parent but still can't get data for tbl_x.id and tbl_y.name.

I get complicacy when try retriving list of tbl_x.id because:

  1. the database was different but still in 1 server and using same username and password
  2. tbl_x only has relation with tbl_a which tbl_x.description = tbl_a.name

here is my add function in Controller:

    public function add() {
    if ($this->request->is('post')) {
        $this->D->create();


        Controller::loadModel('C');
        $data = $this->C->find('first', array(
            'conditions' => array(
                'C.id' => $this->request->data['D']['c_id']
            )
        ));
        $this->request->data['D']['c_name'] = $data['C']['name'];


                    $this->loadModel('X','Y');
                    $x = $this->X->find('list',array(
                        'conditions' => array(
                            'X.x_number' => $this->request->data['D']['x_no']
                        )
                    ));
                    $this->set('X',$x);
                    $y = $this->Y->find('list', array(
                        'conditions' => array(
                            'Y.code' => $this->request->data['D']['y_code']
                        )
                    ));
                    $this->set('Y',$y);

        if ($this->D->save($this->request->data)) {
            $this->Session->setFlash(__('The data has been saved.'), 'success');
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The data could not be saved. Please, try again.'));
        }
    }
    $a = $this->D->A->find('list');

    $types = array(
            'prepaid' => 'Prepaid',
            'postpaid' => 'Postpaid',
            'hybrid' => 'Hybrid'
    );
    $this->set(compact('a', 'types'));
}

here is some of my view

<?php echo $this->Form->create('D'); ?>
<?php
    echo $this->Form->input('a_id', array('empty' => '-- Select  --', 'default' => '-- Select  --'));
    echo $this->Form->input('b_id', array('empty' => '-- Select  --'));
    echo $this->Form->input('c_id', array('empty' => '-- Select  --'));
    echo $this->Form->input('x_no', array('options' => $x,
                'class' => 'span5'));
    echo $this->Form->input('y');
    echo $this->Form->input('type');
?>
<?php echo $this->Form->end(__('Submit')); ?>

Can anyone give me idea how I should do it?

John Conde
  • 217,595
  • 99
  • 455
  • 496
Angelina
  • 53
  • 1
  • 6
  • Check this question out: http://stackoverflow.com/questions/13223946/how-to-use-multiple-databases-dynamically-for-one-model-in-cakephp – Simon Mason Mar 04 '15 at 10:26
  • What is your exact problem? Is your problem `$this->X->find` does not return any results? If so, did you see if the SQL being generated is correct? Also, you need to tell us which version of Cakephp you are using. – AgRizzo Mar 04 '15 at 12:20
  • my problem is when user select a_id then form input x_no show some list of x_no based on a_id. i already did some sql query test and give me what I want. i'using cakephp version 2.6 – Angelina Mar 05 '15 at 04:29
  • Improved code formatting – John Conde Mar 06 '15 at 00:59

0 Answers0