phpcssdatabasecakephptoggleswitch

how does visible two models value in one view and update two models value by one controller


I have two Models Group_ones and Group_twos. I show this value in ac_config.ctp file.

My controller code is below

public function ac_config($id = null)
    {
        if (!$id) {
            $this->Session->setFlash('Please provide a Site id');
            $this->redirect(array('action'=>'dashboard'));
        }

        $site_id_1          = $this->GroupOne->findById($id);
        $site_name          = $site_id_1['GroupOne']['site_name'];
        $ac_one_time        = $site_id_1['GroupOne']['ac_on_time_one'];
        $group_one_active   = $site_id_1['GroupOne']['active'];

        $site_id_2          = $this->GroupTwo->findById($id);
        $ac_two_time        = $site_id_2['GroupTwo']['ac_on_time_two']; 
        $group_two_active   = $site_id_2['GroupTwo']['active'];


        if (!$site_id_1) {
            $this->Session->setFlash('Invalid Site ID Provided');
            $this->redirect(array('action'=>'dashboard'));
        }

        if (!$site_id_2) {
            $this->Session->setFlash('Invalid Site ID Provided');
            $this->redirect(array('action'=>'dashboard'));
        }

        if ($this->request->is('post') || $this->request->is('put')) {
            $this->GroupOne->id = $id;
            $this->GroupTwo->id = $id;

            if (($this->GroupTwo->save($this->request->data)) || ($this->GroupOne->save($this->request->data))) {
                $this->Session->setFlash(__('AC Configuration has been update'));
                $this->redirect(array('action' => 'ac_config', $id));
            }
            else
            {
                $this->Session->setFlash(__('Unable to AC Configuration has been update.'));
            }
        }

        $this->set(compact('site_name','ac_one_time','group_one_active'));
        $this->set(compact('ac_two_time','group_two_active'));

        $this->set('group_one', $site_id_1);
        $this->set('group_two', $site_id_2);

        if (!$this->request->data) {
            $this->request->data = $site_id_1;
            $this->request->data = $site_id_2;  
        }
    }

My view content is below Image_one

Problem occurs when I save button. It only save group-2 form values in database. When AC Run Time and AC Rest Time show Group-2 then form values save in database of the Model Group_twos and AC Run Time and AC Rest Time show Group-1 then form values save in database of the Model Group_ones.

Another problem is below

if (!$this->request->data) {
    $this->request->data = $site_id_1;
    $this->request->data = $site_id_2;  // this show rest time, run time
}

If I write this above code then I get

Image_two

if (!$this->request->data) {
    $this->request->data = $site_id_2;
    $this->request->data = $site_id_1;  //this show rest time, run time.
} 

Again if I write this above code then I get

Image_three

But I need AC Run Time and AC Rest Time both group same time and when I click save button any two group that corresponds group value save in Model. Whats my wrong my logic.

Thanks your help.


Solution

  • Simply merge the two arrays before setting the request data:

    if (!$this->request->data) { $this->request->data = array_merge($site_id_1, $site_id_2); }

    If this does not help, it would be useful to see the code of your view as well.